How can I get the user’s public IP if my server is behind a router?
I’m trying:
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
but all I get is the router gateway.
Apparently, REMOTE_ADDR won’t work in my case and neither will HTTP_X_FORWARDED_FOR.
Any ideas on how to get the user’s public IP in my case?
Thanks.
There is no way of getting it.
But, you can find whether the user is opening the page from a local network or from the web.
If the IP is the IP that the router assigns to the requests coming from the web, then it’s clear it comes from the web.