UserHostAddress gives wrong IPs

后端 未结 7 1628
[愿得一人]
[愿得一人] 2021-01-02 03:26

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 pro

7条回答
  •  鱼传尺愫
    2021-01-02 03:54

    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

提交回复
热议问题