Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

asp.net core - RemoteIpAddress is always null

var test1 = HttpContext.Features.Get<IHttpConnectionFeature>();
var test2 = HttpContext.Connection.RemoteIpAddress;

When running the application locally on IISExpress, these two lines correctly return the value 0:0:1.

When I publish the application on IIS 7.5 (which is running on a VM). RemoteIpAddress is always null

I am using ASP.Net 5 RC 1.

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...