UserHostAddress gives wrong IPs

后端 未结 7 1579
[愿得一人]
[愿得一人] 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:40

    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>
    
    0 讨论(0)
  • 2021-01-02 03:42

    Building on Dave Anderson's answer, here is a snippet that takes into account a chain of reverse proxies.

    string forwardedFor = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
    string ipStr = string.IsNullOrWhiteSpace(forwardedFor) 
                       ? Request.ServerVariables["REMOTE_ADDR"] 
                       : forwardedFor.Split(',').Select(s => s.Trim()).First();
    
    0 讨论(0)
  • 2021-01-02 03:50

    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)?

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-02 04:00

    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.

    0 讨论(0)
  • 2021-01-02 04:02

    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.

    0 讨论(0)
提交回复
热议问题