How do I obtain Client IP address in .net-core instead of the load balancers? (using X-Forwarded-For)

3.5k views Asked by At

I'm simply trying to get the Client IP address in a .net-core controller after they POST. We have a load balancer between the client and the server.

The setup in my startup:

            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders =
                    ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });

And configure:

app.UseForwardedHeaders();

Then I attempt to call the code below, but it returns the load balancer IP address, not the clients IP:

var clientIpAddress = HttpContext.Connection.RemoteIpAddress.ToString();

I have tried everything I can search on this website and the documentation, and nothing seems to work. I've also tried using HttpContext.Request.Headers["X-Forwarded-For"] which returns null / empty.

Our OPS team says the IP isn't being modified by the balancer, and should be in the value of the x-Forwarded-For header. Why is the load balancer sending back it's IP and not the clients IP address?

3

There are 3 answers

0
samwu On

You can try this method to get client IP address:

1.Add the Microsoft.AspNet.HttpOverrides package.

2.n your configure() method add below code.

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

In X-Forwarded-For you will get client ip,proxy1 & proxy2.

Get the first item of the client/user IP, The first item is the original client IP

HttpContext.Current.Request.Headers["X-Forwarded-For"].Split(new 
char[] { ',' }).FirstOrDefault()
0
RohanGarud On

Your settings for the Middlewares are correct and I am also having the same settings.

Same issue we have just faced. We can't get the values in headers for header "X-Forwarded-For" even in controller. After deployment all were gone hanged up and 500 error were there. So I have done one illegal code you can say in _Layout.cshtml and you know in this client side file I can read the headers like X-Forwarded-For,X-Azure-ClientIP and X-Azure-SocketIP. And all can have only one ip address and that is of client's public ip address. And after getting the ip address from Layout page you can store that value in session and redirect to any other page and from that page you can use ip address from session. Below is the code in layout to read the headers.

if (string.IsNullOrEmpty(HttpContextAccessor.HttpContext.Session.GetString("ClientIpAddress")))
{
    Microsoft.Extensions.Primitives.StringValues headerIPAddress = string.Empty;
    HttpContextAccessor.HttpContext.Request.Headers.TryGetValue("X-Forwarded-For", out headerIPAddress);
    HttpContextAccessor.HttpContext.Session.SetString("ClientIpAddress", headerIPAddress.ToString());
    HttpContextAccessor.HttpContext.Response.Redirect("~/Home/Index");
}