为什么ServerVariable [" REMOTE_ADDR"]返回服务器的IP? [英] Why does ServerVariable["REMOTE_ADDR"] returns the server IP?

查看:223
本文介绍了为什么ServerVariable [" REMOTE_ADDR"]返回服务器的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

string ip = Request.ServerVariables["REMOTE_ADDR"];

其中,在测试环境中不会返回该用户的IP addrress,但是当我们的网站部署到生产环境,这个变量的服务器,其中的应用程序托管的IP。任何帮助?

Which, in the test environment does return the user IP addrress, but when we deploy the website to production, this variable has the IP of the server where the application is hosted. Any help?

推荐答案

我的猜测是,有中间的代理。使用 HTTP_X_FORWARDED_FOR 第一,如果这是空,然后用 REMOTE_ADDR

My guess is that there is a proxy in the middle. Use HTTP_X_FORWARDED_FOR first, and if that's null, then use REMOTE_ADDR

MSDN文章

虽然刚刚取回的REMOTE_ADDR服务器变量应该够了,我发现网上资源的建议code这样也应该检查HTTP_X_FORWARDED_FOR变量;如果请求通过了转换地址的代理服务器,它是这个变量包含正确的地址。如果你要求不存在的服务器变量,在ServerVariables属性返回一个空字符串。因此,即使这个属性没有出现在我的测试中,试图检索它的值不会引起麻烦。

Although retrieving just the REMOTE_ADDR server variable should be enough, I found resources online that suggested that code like this should also check the HTTP_X_FORWARDED_FOR variable; if the request comes through a proxy server that translates the address, it's this variable that contains the correct address. If you request a server variable that doesn't exist, the ServerVariables property returns an empty string. Therefore, even though this property doesn't appear in my tests, attempting to retrieve its value doesn't cause trouble.

更新:

如果它是一个负载均衡器,你必须已经设置改变了,你应该问问,看看他们是否能有经过的起源IP。我知道这可以与微软的ISA服务器完成的。

If it's a load balancer that you have have settings changed on, you should ask to see if they can have the origination IP passed through. I know this can be done with Microsoft's ISA server.

如果这不是一个选项,但如果它们产生的结果是,这些其他的服务器变量,你可以试试,看看:

If that's not an option, there are these other server variables that you can try and see if they produce a result:

"HTTP_X_COMING_FROM"
"HTTP_X_FORWARDED_FOR"
"HTTP_X_FORWARDED"
"HTTP_X_REAL_IP"
"HTTP_VIA"
"HTTP_COMING_FROM"
"HTTP_FORWARDED_FOR"
"HTTP_FORWARDED"
"HTTP_FROM"
"HTTP_PROXY_CONNECTION"
"CLIENT_IP"
"FORWARDED"

这篇关于为什么ServerVariable [" REMOTE_ADDR"]返回服务器的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆