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?
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.