Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Is it possible to get client machine Location without the IP address? I tried different codes to get the client machine IP address, but none of them is giving the correct IP, either it give 127.0.0.1 Or 10.0.0.3.

What I have tried:

I tried the below two methods
1)
private string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}

return Request.ServerVariables["REMOTE_ADDR"];
}

This will give 127.0.0.1

2)

public static string getExternalIp()
{
try
{
string externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(externalIP)[0].ToString();
return externalIP;
}
catch { return null; }
}

This will get the correct IP, but it takes long time to get the Out put, Is there any code which will get the IP very fast.
Posted
Updated 21-May-18 9:29am

No - and even with the IP address, it doesn't tell you anything useful: Using IP based Geolocation - and why it's pretty much useless.[^]
 
Share this answer
 
You've tagged this as ASP.NET, which means that method 2 will return the external IP address of the server.

Method 1 is returning the loopback address because you're debugging the code in Visual Studio. In that particular instance, the server and client are the same computer, so 127.0.0.1 is the correct address.

Once you deploy your code to a real server, method 1 should return the correct external IP address of the client.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900