Get a users remote IP Address in ASP.Net Core


Issue

You want to obtain the IP address of the user who is accessing your site in an ASP.Net Core website.

Resolution:

In ASP.Net Core you can still obtain remote IP address from the HttpContext but it has moved locations from older iterations of MVC/Webforms. If you are in a controller you can access it with a single line (the HttpContext exists in controllers by default, if you are not in a controller you can still access this but you will need to inject the HttpContext).

C#

// As the IpAddress object
var ipAddress = HttpContext.Connection.RemoteIpAddress;