1

I'm trying to get the client's IP address of a User. My code is returning the server's (host) IP address.

string strHostName = System.Net.Dns.GetHostName()
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string ip = addr[6].ToString();
return ip;

I need IP address of the client, not a server IP.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Vishwa
  • 11
  • 2

1 Answers1

1

Look at Request.UserHostAddress. Just remember that if your user is behind a NATd network (i.e. enterprise, or mobile) the IP may not uniquely identify a user. Use cookies, session variable etc. to achieve that.

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • i tried this, I m getting ::1 as output, but i m looking private IP address of client machine .i.e. 192.168.3.0 something like that, – Vishwa Jun 18 '19 at 20:12
  • please help me out in this – Vishwa Jun 18 '19 at 20:12
  • ::1 and 127.0.01 are your localhost IP. If you are accessing your server on the local machine, that's the result you can expect. Wither test the client and server on machines in different networks, or use an external IP location service. – Traveling Tech Guy Jun 18 '19 at 22:31