How do I capture the real IP / Public IP of the user who is accessing the application? The IP of the computer I can capture normally, but I want to know the public IP of that client.
How do I capture the real IP / Public IP of the user who is accessing the application? The IP of the computer I can capture normally, but I want to know the public IP of that client.
This is an accessible variable in any < in> Action :
Request.UserHostAddress;
Or else:
Request.ServerVariables["REMOTE_ADDR"]
I get it like this:
public static string GetPublicIP()
{
string url = "http://checkip.dyndns.org";
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd().Trim();
string[] a = response.Split(':');
string a2 = a[1].Substring(1);
string[] a3 = a2.Split('<');
string a4 = a3[0];
return a4;
}
add using
using System.Net;
include the code below
string host = Dns.GetHostName();
string ip = Dns.GetHostAddresses(host)[2].ToString();