8

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

Rohit Jadhav
  • 1,035
  • 2
  • 11
  • 14
  • 1
    Does this answer your question? [Asp Net Web API 2.1 get client IP address](https://stackoverflow.com/questions/22532806/asp-net-web-api-2-1-get-client-ip-address) – Kiya Jan 13 '20 at 06:55
  • Does 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) – Sayyed Dawood Nov 01 '21 at 05:47

2 Answers2

11

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()
elahe karami
  • 641
  • 5
  • 11
2
string ipAddress = HttpContext.Connection.RemoteIpAddress.ToString(); 

deviceName = Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress).HostName;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
KingRaja
  • 154
  • 1
  • 4