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;

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.