<p>I have an ASP.NET Core webapi running in an on-prem bare-metal Kubernetes cluster. There's no external load-balancer, and I'm using NGINX ingress.</p>

<p>I want to get the users' IP address, and am using <code>HttpContext.Connection.RemoteIpAddress</code> in the .NET code.</p>

<p>Unfortunately, this is picking up the IP address of the nginx ingress (possibly the controller given the namespace)...</p>

<p><code>::ffff:10.244.1.85</code></p>

<p>Doing a reverse DNS lookup resolves that to...</p>

<p><code>10-244-1-85.ingress-nginx.ingress-nginx.svc.cluster.local</code></p>

<p>After a little bit of Googling, I tried adding <code>externalTrafficPolicy: "Local"</code> to my service definition, but that didn't make a difference.</p>

<p>This seems like something that should be really trivial and quite a common requirement. Any ideas?</p>

<p>First try to get ip from header <code>X-Forwarded-For</code> as shown below, if it's null then you can use Connection.RemoteIpAddress. Also make sure your nginx configmap has proxy enabled as per screenshot:<br />
<a href="https://i.stack.imgur.com/BwW1l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BwW1l.png" alt="enter image description here" /></a></p>
<pre><code>var ip = IPAddress.Parse(_accessor.ActionContext.HttpContext.Request.Headers[&quot;X-Forwarded-For&quot;].FirstOrDefault());
            if (string.IsNullOrEmpty(ip.ToString()))
            {
                ip = _accessor.ActionContext.HttpContext.Connection.RemoteIpAddress.MapToIPv4();
            }
</code></pre>
