5

How can I get the user's public IP if my server is behind a router?

I'm trying:

protected string GetIPAddress() 
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }
    return context.Request.ServerVariables["REMOTE_ADDR"]; 
}

but all I get is the router gateway.

Apparently, REMOTE_ADDR won't work in my case and neither will HTTP_X_FORWARDED_FOR.

Any ideas on how to get the user's public IP in my case?

Thanks.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • 5
    Dump all incoming HTTP headers. If the desired value is not there you have lost. If it is there you can see how to get to it. – usr Nov 01 '12 at 12:33
  • Sorry, but I do not understand what do you actually mean by `behind a router`. I have managed to get the `public IP` from websites such as `checkip.dyndns.org` downloading a string and reading it but I do not actually understand what do you mean by `behind a router`. May you please clarify? :) – Picrofo Software Nov 01 '12 at 12:35
  • What make and model is your router? Have you tried to change its configuration to pass through the IP address of the client? – David Waters Nov 01 '12 at 12:37
  • 1
    @PicrofoEGY it sounds like the question refers to a reverse-proxy at the web-server's end; you need to be careful not to constantly report the reverse-proxy's address as the user's address – Marc Gravell Nov 01 '12 at 12:38
  • @MarcGravell Thank you for providing the information. Have a great day :) – Picrofo Software Nov 01 '12 at 12:39
  • The Client IP was not on the HTTP header.. @usr –  Nov 01 '12 at 12:39
  • @user1761123 I think you need to be a little bit more precise about the network topology here; *whose* router/proxy are we talking about? the client's? or the web-farm's? – Marc Gravell Nov 01 '12 at 12:41
  • @MarcGravell Unfortunately I don't own the server, but I was asked to develop a website that tracks if the user's IP is between a certain range or not. While I tried the above code,I noticed that I always get the same IP no matter where I am opening the site from, and this led me to the concussion that the IP was in fact the router's gateway. –  Nov 01 '12 at 12:45
  • @user1761123 Did you check the other HTTP Headers? As Marc said, the IP address of the remote endpoint might have been added under there. – Anirudh Ramanathan Nov 01 '12 at 12:51
  • Yes, this is what I got: Connection=keep-alive&Accept=text%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2c*%2f*%3bq%3d0.8&Accept-Charset=ISO-8859-1%2cutf-8%3bq%3d0.7%2c*%3bq%3d0.3&Accept-Encoding=gzip%2cdeflate%2csdch&Accept-Language=en-US%2cen%3bq%3d0.8&Cookie=ASP.NET_SessionId%3d1031dt55mokvsgihjp2113q3&Host=stackoverflow.com&User-Agent=Mozilla%2f5.0+(Windows+NT+6.1)+AppleWebKit%2f537.4+(KHTML%2c+like+Gecko)+Chrome%2f22.0.1229.94+Safari%2f537.4 –  Nov 01 '12 at 12:53

3 Answers3

2

You are trying to deal with IP & Routing at the HTTP level.

At the Application Layer, the packet has already traversed up the Routing/IP layer and that information has been stripped out.

The packet you see at the HTTP level is exactly as sent out by the remote end-point at his Application Layer level, which was devoid of IP information, so looking in the HTTP headers won't help IMHO.

EDIT: As @Marc Gravell said, some Reverse proxies do add this information to HTTP headers. So you should check yours (if you have one configured), if it does that.

This answer lists some other server-variables you could check.

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • that all depends on the infrastructure being used; a lot of reverse-proxies etc **will add this information** (as http headers in the request) – Marc Gravell Nov 01 '12 at 12:39
0

The only reliable way is to reach out to some known service on the internet that will tell you what IP it sees your request coming from. Of course, that service has to continue functioning and be reachable or this feature breaks. For example, http://whatismyip.com

John Watts
  • 8,717
  • 1
  • 31
  • 35
  • I have tried http://jsonip.appspot.com/ which works fine, but I need a C# APS.NET approach. –  Nov 01 '12 at 12:40
  • Correct. So you need to do some cross-domain hackery to make the user's browser visit a site like this and return you the result. – John Watts Nov 01 '12 at 12:51
  • By the way, I'm not saying this is good or the "right" way to figure out where the user's request comes from. I'm just saying that in a hosted environment there may literally be no other way than taking advantage of some other service on the internet that isn't behind a proxy that hides the user's IP. – John Watts Nov 01 '12 at 12:55
0

There is no way of getting it.

But, you can find whether the user is opening the page from a local network or from the web.

If the IP is the IP that the router assigns to the requests coming from the web, then it's clear it comes from the web.