I Use this code to get the public IP-address (thanks to this post How to get the IP address of the server on which my C# application is running on?):
public static string GetPublicIP()
{
try
{
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
return direction;
}
catch (Exception ex)
{
return "127.0.0.1";
}
}
But no matter who that access my website, they all get the same IP, and it is the server public IP, not the current user's IP.
Is it possible to run the WebRequest in context of the current user, not as the server?
Or is the problem that I run this function inside App_Code so that the current user Request is not available, instead it use the server context?
Please help!