How can we get Client IP in .net core 6 web api. I have already implemented this in .net core 3, and 4 and never faced the problem, For some reason the code is not working in .net core 6, Instead of client IP, the code is giving Server IP. I have hosted this on an IIS.
Below is the code from program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
//rest of the code follows
var app = builder.Build();
app.UseForwardedHeaders();
//rest of the code follows
Below is the code from the controller
string ip_address = Request.HttpContext.Connection.RemoteIpAddress?.ToString()
The above line is always giving me the IP address of the server where this web api is deployed, instead I need to IP address of the client from where the request has come.
Please let me know what is wrong here.
================================================================ Based on the early responses I received, Adding more information my question, My Web API is hosted behind a company firewall and the API is accessed from several networks outside of the company.