2021年1月28日星期四

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

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?

https://stackoverflow.com/questions/65947201/how-do-i-obtain-client-ip-address-in-net-core-instead-of-the-load-balancers-u January 29, 2021 at 08:34AM

没有评论:

发表评论