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 an application on IIS 7.5 (which runs on a virtual machine). RemoteIpAddress always null

I am using ASP.Net 5 RC 1.

How can I get client IP address in ASP.NET 5 application?

I tried the solutions in the following questions, however I have the problem mentioned above:

  • Question 1
  • Question 2
+7
asp.net-core asp.net-core-mvc
Feb 16 '16 at 19:34
source share
2 answers

What you see is more accurate, if not useful. The connection is terminated in IIS, which then forwards to Kestrel, the v.next web server, so the connections to the web server do come from localhost.

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

  • Add Microsoft.AspNet.HttpOverrides Package
  • In your Configure() method add

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

Please note that this package will change in RC2 to make it more secure.

+7
Feb 16 '16 at 20:30
source share

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

 app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); 
0
Nov 20 '17 at 13:29
source share



All Articles