<p>I have an asp.net core 7.0 api application in a docker container hosted in Kubernetes behind an Nginx ingress controller.</p>
<p>To get the client ip address i'm using</p>
<blockquote>
<p>context.HttpContext.Connection.RemoteIpAddress</p>
</blockquote>
<p>for all user requests I get a Private Ip address like '10.244.0.1'</p>
<p>In such instances i'm expecting Public IP address</p>

<p>You need to configure nginx to <a href="https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-7.0#nginx-configuration" rel="nofollow noreferrer">forward the <code>X-Forwarded-For</code> and <code>X-Forwarded-Proto</code> headers</a>. Example:</p>
<pre><code>server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
</code></pre>
<p>Then setup the <code>UseForwardedHeaders</code> middleware. This middleware will update <code>HttpContext.Connection.RemoteIpAddress</code> using the <code>X-Forwarded-For</code> header value.</p>
<pre class="lang-cs prettyprint-override"><code>var builder = WebApplication.CreateBuilder(args);

// ...
builder.Services.Configure&lt;ForwardedHeadersOptions&gt;(options =&gt;
{
    options.ForwardedHeaders =
        ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});

var app = builder.Build();

app.UseForwardedHeaders();

// ...
</code></pre>
<p>Check: <a href="https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-7.0#forwarded-headers" rel="nofollow noreferrer">Configure ASP.NET Core to work with proxy servers and load balancers</a></p>
