RemoteIpAddress is always null

后端 未结 3 1753
情深已故
情深已故 2020-12-17 22:15
var test1 = HttpContext.Features.Get();
var test2 = HttpContext.Connection.RemoteIpAddress;

When running the applicat

相关标签:
3条回答
  • 2020-12-17 22:29

    In ASP.NET Core 2.0 project you need to add package Microsoft.AspNetCore.HttpOverrides and add this code to your Configure method:

    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });
    
    0 讨论(0)
  • 2020-12-17 22:39

    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.

    What you need to do is enable support for X-Forwarded-For

    1. Add the Microsoft.AspNet.HttpOverrides package
    2. In your Configure() method add

          app.UseOverrideHeaders(new OverrideHeaderMiddlewareOptions
          {
              ForwardedOptions = ForwardedHeaders.XForwardedFor | 
                                 ForwardedHeaders.XForwardedProto
          });
      

    Note that this package will change in RC2 to make it safer to use.

    0 讨论(0)
  • 2020-12-17 22:50

    That is because of you are trying to connect from localhost. If you have another computer in same LAN, try to connect with this pc but use user ip instead of localhost.

    0 讨论(0)
提交回复
热议问题