UserHostAddress gives wrong IPs

2019-02-09 04:50发布

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.

7条回答
孤傲高冷的网名
2楼-- · 2019-02-09 05:40

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"].Split(",")[0];

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

查看更多
登录 后发表回答