Request.UserHostAddress返回负载均衡器的IP地址 [英] Request.UserHostAddress return IP address of Load Balancer

查看:92
本文介绍了Request.UserHostAddress返回负载均衡器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站中有一条关键的代码行,该代码行在我们的开发环境中有效,但不适用于生产环境.好吧,我说它可以在开发中工作,但事实是它给出了::1,这是IPv6环回地址.

I have a critical line of code in my site that worked in our development environment but not on production. Well, I say it worked in development but the truth is it gave ::1, which is the IPv6 loopback address.

无论如何,我想做的就是捕获访问该站点的用户的IP地址.因此,我使用Request.UserHostAddress来做到这一点.正如我所说,在开发中,它给了我回送地址,这是正确的,因为我是在计算机上运行该站点的.在现场,它做了完全不同的事情.它总是返回负载均衡器的地址.

Anyway, what I wanted to do was capture the IP address of the user who came to the site. I therefore, used Request.UserHostAddress to do that. On development, as I said, it gave me the loopback address, which is correct, since I was running the site from my machine. On live it did something entirely different. It always returned the address of the load balancer.

我想了解的是这个.我使用Request.UserHostAddress捕获用户的IP地址是否错误,或者我们的网络设置是否有问题或其他原因?

What I am trying to understand is this. Was I wrong to use Request.UserHostAddress to capture the user's IP address or is there something wrong with our network setup or something else?

谢谢

Sachin

推荐答案

在您自己的应用程序中,如果没有采取任何其他措施来帮助您,您将陷入困境.那就是您所能获得的尽可能多的信息.

From within your own application, if nothing else has been done to help you, you're stuck. That's as much information as is available to you.

如果幸运的话,您的负载均衡器已配置为添加一个或多个额外的标头,其中包含有关原始请求的信息.

If you're lucky, your load-balancer has been configured to add one or more extra headers with information about the original request.

一种常见的解决方案是 X-Forwarded-For 标头:

One common solution is the X-Forwarded-For header:

X-Forwarded-For(XFF)HTTP标头字段是一种事实上的标准,用于标识通过HTTP代理或负载平衡器连接到Web服务器的客户端的原始IP地址.

The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer.

,然后可以通过Request.Headers属性访问.

which you would then access via the Request.Headers property.

但是,发现这个(或另一个)标题是否可用不是我们可以帮助的-您需要与为您的组织配置负载平衡器的人员交谈.

But discovering whether this (or another) header is available is not something we can help with - you need to talk to the people who configured the load balancer for your organization.

这篇关于Request.UserHostAddress返回负载均衡器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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