Client IP Address Mismatch between ASP.NET 5 Applications and IIS Logs

We are writing an ASP.NET 5 (vNext) application that partially includes geocoding a user's location based on their IP address. We are trying to capture the client IP address using the following code, found from several examples on the Internet:

var connection = context.HttpContext.GetFeature<IHttpConnectionFeature>();
if (connection != null) {
    clientIpAddress = connection.RemoteIpAddress.ToString();
}

When we register clientIpAddressthat comes from IHttpConnectionFeature, we find that the IP address is incorrect.

However, we notice that IIS is fixing the correct client IP address. We see this by checking requests through the IIS control panel.

So, somewhere between the IIS request and the code of our application, the IP address of the client changes, or our application code does not work properly with IIS. Or it’s possible that IIS does additional work to decrypt the client’s IP address — the work that we must emulate in our application code.

Why is the client IP address correct in the IIS request logs, but incorrect in our application code above? How can we get the correct client IP address in our code?

Relevant software versions:

  • ASP.NET 5 (vNext)
  • KRE 1.0.0-beta2
  • IIS 8

Update . We ran the .NET 4.5 application on the same IIS server, which retrieves the IP address using Request.UserHostAddress, and also retrieves the correct IP address (the same IIS log), so this seems to be a problem with the new .NET 5 code.

+4
1

- Microsoft.AspNet.Loader.IIS. , Microsoft .

. Github: https://github.com/aspnet/HttpAbstractions/issues/181

0

All Articles