I'm using ASP.NET Core 3.1 Web API. I'd like to get the user's Ip address. I have been stuck for a long time, please help me, thank you so much!
-
1Does this answer your question? [How do I get client IP address in ASP.NET CORE?](https://stackoverflow.com/questions/28664686/how-do-i-get-client-ip-address-in-asp-net-core) – thebugsdontwork Oct 05 '21 at 14:56
2 Answers
@Abdelkrim's answer is sufficient if you want to use IPv6 addresses, which are more unique by definition and therefore more secure. However IPv4 addresses are still more common than IPv6 so it becomes an accessibility versus security problem (accessibility wins btw, don't want your app inaccessible to half the world).
To use IPv4 addresses however, you can simply extend the code in Abdelkrim's answer as follows:
var ipv4 = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
That will return any IP address as ^[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}
, i.e. as IPv4 format.
As for your confusion with your localhost IP address: When developing any app which runs on a network (an API, website, et cetera) locally, the instance will run inside your PC so to speak. This inside of your PC is represented by the ::1 IP address, or 0.0.0.1, or 0:0:0:0:0:1. The more commonly known localhost address is 127.0.0.1.
This address is completely restricted to your own system and does not communicate with the outside.
The command ipconfig
in cmd returns the IP address your PC uses to communicate with the outside. This IP address is completely unrelated to localhost, nor do either care about each other's existence.
So, the website you used to get your PC's IP address will return the one your PC uses to communicate with the outside world (the one returned by ipconfig
as well), not your localhost IP.
Hopefully the answer for your coding problem is helpfull and the explanation about localhost is clear!

- 401
- 3
- 17
In the controller you can call :
var remoteIpAddress = HttpContext.Connection.RemoteIpAddress.ToString();
You could have obtained an answer by a simple Google search.

- 1,083
- 8
- 18
-
-
-
-
-
-
-
4And therefore the IP address is "::1" = localhost as IPv6 address. – Klaus Gütter Oct 05 '21 at 14:33
-
1
-
2If you want the IPv4 address instead, you could call `...RemoteIpAddress.MapToIPv4().ToString();`. ::1 will then become 0.0.0.1. – thebugsdontwork Oct 05 '21 at 14:38
-
I run "ipconfig" command in cmd, I got my ip v6 is 2001:ee0:512f:1d20:e96a:5a67:887d:c09d, not 0:0:0:0:0:1. Even I use https://whatismyipaddress.com/ to get my IP, the result is not 0:0:0:0:0:1 – Võ Thái Phúc Oct 05 '21 at 14:39
-
Yes, but Postman is communicating with your server through the loopback network. – Abdelkrim Oct 05 '21 at 14:40
-
1`ipconfig` returns the IP address of your system, your localhost runs internal on your PC and doesn't communicate outward. The IP you got is the one your PC uses to communicate with the outside world – thebugsdontwork Oct 05 '21 at 14:40
-
Here is what you can do to convince yourself that it's the right solution. Try to serve your web application and then try to access it from a device that's in the same network using your device IP address 2001:ee0...etc – Abdelkrim Oct 05 '21 at 14:42