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

查看:601
本文介绍了RemoteIpAddress始终为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

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

  1. Add the Microsoft.AspNet.HttpOverrides package
  2. 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屋!

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