views:

1345

answers:

5

I collect statistics on IP addresses from where users visit my site and I have noticed what there are only two IP addresses presented, 172.16.16.1 and 172.16.16.248. The property I use to determine IP address is

Request.UserHostAddress

What could be a reason of IP address information losing? All the users are from around the world, so they cann't be behind only two proxies.

+2  A: 

I assume you are behind a NAT/Reverse Proxy so I think you have to use:

Request.ServerVariables("REMOTE_ADDR")

Most likely 172.16.0.0/12 is your privat LAN where 172.16.16.248 is your own address and 172.16.16.1 the address of your router/proxy.

Node
You didnt mean /16?
leppie
Nope, /12 should be right as default. =)http://en.wikipedia.org/wiki/Private_network
Node
Thanks for the info, it's been more than 10 years that I last worked in networking! :)
leppie
+2  A: 

This looks like the work of a reverse proxy. When you use a reverse proxy, the client connects to the proxy, which itself opens a new connection to your server. Since ASP.NET uses the infos of the incoming connection to fill the user address, you get the address of the reverse proxy.

If you are indeed in this configuration, you'll need help from the reverse proxy to get the right information. Most reverse proxies offer the possibility to add a header to the HTTP request, with the real IP address of the client. Check the documentation of your proxy.

Mathieu Garstecki
A: 

The two addresses you've listed there are from one of the ranges defined as being private. (see here for description)

It sounds more like you're picking up the internal address of your own firewall(s)?

Andrew Edgecombe
+6  A: 

You might want to something like this;

string SourceIP = String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? Request.ServerVariables["REMOTE_ADDR"] : Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

The HTTP_X_FORWARDED_FOR header gets the IP address behind proxy servers.

See this page that explains why in more detail; Getting The Real IP of your Users

Dave Anderson
I know about HTTP_X_FORWARDED_FOR header, but I am more interested in reasons of the problem. Thanks anyway!
Alexander Prokofyev
+1 for linking to my article!
Oli
Beware that chained proxies can lead to several HTTP_X_FORWARDED_FOR headers.
Marc Climent
+1  A: 

Request.ServerVariables("REMOTE_ADDR") isn't work. this problem is because ur server is probably behind some proxy.(or connected to internet via some network) or your router settings are set as NAT (Network Address Translation) this technique doesnt pass ip to server. in such situations u can't get IP address using Asp.net however Java Provide applet using which u can get IP Address in any case.

( for Netscape, Mozilla and Firefox only, and Java must be enabled)

<script language="javascript" type="text/javascript">   

if (navigator.appName.indexOf("Netscape") != -1){
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
document.write("<b>Your IP address is " + ip+'</b>');
}
else {
document.write("<b>IP Address only shown in Netscape with Java enabled!</b>");
}

</script>
Thanks for information.
Alexander Prokofyev