RemoteIpAddress 始终为空 [英] RemoteIpAddress is always null

查看:55
本文介绍了RemoteIpAddress 始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

推荐答案

您所看到的是准确的,如果没有用的话.IIS 的连接终止,然后转发到 Kestrel,即 v.next Web 服务器,因此与 Web 服务器的连接确实来自 localhost.

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

  1. 添加Microsoft.AspNet.HttpOverrides
  2. 在你的 Configure() 方法中添加

    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 始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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