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

查看:34
本文介绍了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?

谢谢,

萨钦

推荐答案

在您自己的应用程序中,如果没有做任何其他事情来帮助您,您就会陷入困境.这就是您可以获得的信息.

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 代理或负载平衡器连接到网络服务器的客户端的原始 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天全站免登陆