System.Web.HttpContext.Current.Request.UserHostAddress;

Sam*_*mer 4 c# asp.net ip-address

我使用以下代码来获取用户IP地址,但发生了一些奇怪的事情.我,每次都收到相同的IP地址,无论我是在桌面上还是在我的iPhone上.我在我的网络上,当我从桌面上点击页面时,网络服务器就在那里,但在我的iPhone上,我甚至不在大楼里.我以前没遇到过这个问题,我要回的IP地址是分配给Web服务器的内部地址.

string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
Response.Write(ip);
Run Code Online (Sandbox Code Playgroud)

在我的网络之外我仍然得到相同的IP地址172.16.0.22这是一个路由器地址,而不是我们的外部IP.有没有办法获取外部IP地址.

Dot*_*ser 5

请参阅此链接http://thepcspy.com/read/getting_the_real_ip_of_your_users/

        // Look for a proxy address first
        _ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        // If there is no proxy, get the standard remote address
        if (_ip == null || _ip.ToLower() == "unknown")
            _ip = Request.ServerVariables["REMOTE_ADDR"];
Run Code Online (Sandbox Code Playgroud)