10

I try to get remote (client) IP addres:

var ip = httpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress

But it works only for local requests (it will return ::1 value)

When I load page from remote machine the value is null. I investigated there is no IHttpConnectionFeature in the Features collection in this case.

Why? And how to get remote ip address correctly?

hcp
  • 3,268
  • 6
  • 26
  • 41
  • 1
    There are several issues with it as far as I know, especially if you host it behind a loadbalancer and/or iis. There is a `UseOverrideHeaders` or so but will be renamed to `UseForwardedHeaders` in rc2, see the announcement https://github.com/aspnet/Announcements/issues/147. But as far as I understand, this may not help you neither, because it returns the last X-Forwarded-For which in many cloud deployments would be the reverseproxy and not the original user. – Tseng Apr 01 '16 at 11:29
  • But there is a feature in discussion as far as I know to allow to determine from which hop to get the X-Forwarded-For (i.e. 2 hops back rather than just the latest – Tseng Apr 01 '16 at 11:29
  • I think this is a great answer http://stackoverflow.com/a/36316189/625581 – Martín Coll Oct 11 '16 at 18:01
  • 3
    Possible duplicate of [How do I get client IP address in ASP.NET CORE?](http://stackoverflow.com/questions/28664686/how-do-i-get-client-ip-address-in-asp-net-core) – jgauffin Dec 22 '16 at 13:07

2 Answers2

26

I know that this post is old but I came here looking for the same question and finnaly I did this:

On project.json add dependency:

"Microsoft.AspNetCore.HttpOverrides": "1.0.0"

On Startup.cs, in the Configure method add:

  app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor |
            ForwardedHeaders.XForwardedProto
        });  

And, of course:

using Microsoft.AspNetCore.HttpOverrides;

Then, I got the ip like this:

Request.HttpContext.Connection.RemoteIpAddress

In my case, when debugging in VS I got always IpV6 localhost, but when deployed on an IIS I got always the remote IP.

Some useful links: How do I get client IP address in ASP.NET CORE? and RemoteIpAddress is always null

The ::1 may be because:

Connections termination at IIS, which then forwards to Kestrel, the v.next web server, so connections to the web server are indeed from localhost. (https://stackoverflow.com/a/35442401/5326387)

Community
  • 1
  • 1
Johna
  • 2,623
  • 3
  • 21
  • 36
  • when you find similar questions, and post similar answers, you might want to add a cross-reference between the two. Especially if one seems more complete than the other. HTH – superjos Nov 06 '17 at 18:23
  • @superjos what do you mean? – Johna Nov 06 '17 at 20:46
  • There's another SO question [here](https://stackoverflow.com/a/41335701/540776) which is very close to this one and where you basically copied the same answer. It might be useful in such cases to point one question to another. Maybe even flag question for closing. – superjos Nov 06 '17 at 21:35
  • 2
    Yes, the answer is the same because I answered both questions. I was looking for the same and when i found the solution, I answered both questions (i didn't mark as duplicated because both where asked a time ago and both already had different answeres that didn't solve the problem) – Johna Nov 07 '17 at 15:04
  • This also helps when using ngrok. WIthout this, the RemoteAddress is always ::1. – user3625699 Apr 24 '23 at 21:53
4

Just try this:

var ipAddress = HttpContext.Connection.RemoteIpAddress;

And if you have another computer in same LAN, try to connect with this pc but use user ip instead of localhost. Otherwise you will get always ::1 result.

Pang
  • 9,564
  • 146
  • 81
  • 122
Emre SOLUĞAN
  • 193
  • 1
  • 12
  • Thanks man, I was wondering why it always show ::1 result, and your remark helped me to troubleshoot the problem. Cheeeers =] – chungonion Sep 07 '20 at 08:56