RemoteIpAddress始终为null [英] RemoteIpAddress is always null
问题描述
var test1 = HttpContext.Features.Get<IHttpConnectionFeature>();
var test2 = HttpContext.Connection.RemoteIpAddress;
在 IISExpress 上本地运行应用程序时,这两行正确返回值0:0:1
.
When running the application locally on IISExpress, these two lines correctly return the value 0:0:1
.
当我在 IIS 7.5 (在VM上运行)上发布应用程序时. RemoteIpAddress
始终为null
When I publish the application on IIS 7.5 (which is running on a VM). RemoteIpAddress
is always null
我正在使用 ASP.Net 5 RC 1 .
如何在 ASP.NET 5 应用程序中获取客户端的IP地址?
How can I get the client's IP address in an ASP.NET 5 application ?
我尝试了以下问题中的解决方案,但是我遇到了上述问题:
I tried the solutions in the following questions, however I have the problem mentioned above:
- Question 1
- Question 2
推荐答案
您看到的内容是准确的,如果没有用的话. IIS处的连接终止,然后将其转发到v.next Web服务器Kestrel,因此与Web服务器的连接确实来自本地主机.
What you're seeing is accurate, if not useful. Connections termination at IIS, which then forwards to Kestrel, the v.next web server, so connections to the web server are indeed from localhost.
您需要做的是启用对X-Forwarded-For的支持
What you need to do is enable support for X-Forwarded-For
- 添加
Microsoft.AspNet.HttpOverrides
包 -
在您的
Configure()
方法中添加
- Add the
Microsoft.AspNet.HttpOverrides
package In your
Configure()
method add
app.UseOverrideHeaders(new OverrideHeaderMiddlewareOptions
{
ForwardedOptions = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
请注意,此软件包将在RC2中进行更改,以使其使用更安全.
Note that this package will change in RC2 to make it safer to use.
这篇关于RemoteIpAddress始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!