C# System.Web.HttpContext.Current.Request.UserHostAddress;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9726576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Web.HttpContext.Current.Request.UserHostAddress;
提问by Sam Cromer
I am using the following code to obtain the user I.P. address but something strange is happening. I am, receiving the same ip address every time no matter if I am on my desktop or on my iPhone. I am on our network where the web servers live when I hit the page from my desktop but on my iPhone I was not even in the building. I have not run into this issue before, the ip address that I am getting back is an internal one assigned to the web servers.
我正在使用以下代码获取用户 IP 地址,但发生了一些奇怪的事情。无论我是在台式机上还是在 iPhone 上,我每次都收到相同的 IP 地址。我在我们的网络上,当我从桌面点击页面时,网络服务器就在那里,但在我的 iPhone 上,我什至不在大楼里。我以前没有遇到过这个问题,我取回的 IP 地址是分配给 Web 服务器的内部地址。
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
Response.Write(ip);
Outside of my network I still get the same ip address 172.16.0.22 which is a router address, not our external ip. Is there a way to obtain the external ip address.
在我的网络之外,我仍然得到相同的 ip 地址 172.16.0.22,这是一个路由器地址,而不是我们的外部 ip。有没有办法获取外部IP地址。
回答by DotNetUser
see this link http://thepcspy.com/read/getting_the_real_ip_of_your_users/
请参阅此链接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"];
回答by maycil
Try this;
尝试这个;
if (!String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]))
ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
else
ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
Request.UserHostAddressreturn server IP which IIS run on.
Request.UserHostAddress返回运行 IIS 的服务器 IP。
回答by Sanjay Zalke
I hope this will be resolved by this time, I ran into same issue. Like you we I knew the IP address was one of the server on the network (Load Balancer).
我希望这会在这个时候得到解决,我遇到了同样的问题。像您一样,我们知道 IP 地址是网络上的服务器之一(负载均衡器)。
If your solution is load balanced web servers, then there are high chances that your load balancer is updating the request header information, rerouting through some software load balancer or something.
如果您的解决方案是负载平衡的 Web 服务器,那么您的负载平衡器很可能正在更新请求标头信息,通过某些软件负载平衡器或其他方式重新路由。
In case on local testing, update the router configuration to pass the request details. read some router configuration manual, there should be something which will have this setting. :)
在本地测试的情况下,更新路由器配置以传递请求详细信息。阅读一些路由器配置手册,应该有一些东西会有这个设置。:)
Supply your router information, someone will have router/hardware knowledge on this.
提供您的路由器信息,有人会对此有路由器/硬件知识。
I am not a hardware expert, but ask your network administrator to either pass-on the header info as is or at least update "X-Forwarded-For" information. So you can build code to read this information consistently.
我不是硬件专家,但请您的网络管理员按原样传递标头信息或至少更新“X-Forwarded-For”信息。因此,您可以构建代码以一致地读取此信息。
Find more information about the traffic routing techniques used in industry and issues here.
在此处查找有关行业中使用的流量路由技术和问题的更多信息。
Sanjay
桑杰

