RemoteIpAddress is always null

12,069

Solution 1

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.

Solution 2

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
});

Solution 3

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.

Share:
12,069
A-Sharabiani
Author by

A-Sharabiani

Software Developer, Enthusiast. Interested in C++, C#, JavaScript, Android, Graphics. Check this out!

Updated on June 13, 2022

Comments

  • A-Sharabiani
    A-Sharabiani almost 2 years
    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:

  • A-Sharabiani
    A-Sharabiani about 8 years
    Still getting null. Apparently this is due to a bug: github.com/aspnet/IISIntegration/issues/…
  • blowdart
    blowdart about 8 years
    Ah darn. Move to IIS8? grin
  • CB-Dan
    CB-Dan over 7 years
    The nuget package is now Microsoft.AspNetCore.HttpOverrides and the configure method is: app.UseForwardedHeaders(...)