'How do I get client IP address in ASP.NET CORE 3.1?

how to get a client IP address in ASP.NET 3.1 when writing code in Web API project



Solution 1:[1]

Use this tutorial. wish it will help you. Get client IP address in Asp.NET core 3

Or just use this line of code in your controller:

var ip =HttpContext.Connection.RemoteIpAddress.ToString()

Solution 2:[2]

string ipAddress = HttpContext.Connection.RemoteIpAddress.ToString(); 

deviceName = Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress).HostName;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 elahe karami
Solution 2 Suraj Rao