0

Before anything I know this question is duplicated but let me tell about my prob

As This Link demonstrated the Request.UserHostAddress only captures the IP address of the proxy server or router for this reason I have used this code to return users IP Address :

  string ip = String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? Request.ServerVariables["REMOTE_ADDR"] : Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];

but it always return 172.30.1.1 when I connect with my PC, in the other side my Phone is connected to Telecommunication 3G network when I connected by phone that returns 172.30.1.1 again. I also use this code:

  public static string GetIP4Address()
    {
        string IP4Address = String.Empty;

        foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        if (IP4Address != String.Empty)
        {
            return IP4Address;
        }
        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        return IP4Address;
    }

but results are same.

so How can I return the real IP address of users?

Thanks in advance.

Community
  • 1
  • 1
Aria
  • 3,724
  • 1
  • 20
  • 51

1 Answers1

0

Edit: Misunderstood the question previously.

This is indeed an incorrect lookup and why it is returning the proxy/router ip addresses Request.ServerVariables("REMOTE_ADDR") is what sites like rapidshare use to limit downloads as a group, and not really useful.

What you want to be doing is comparing the HTTP_X_FORWARDED_FOR variable against it, to check that there isn’t a non-transparent proxy in the way.

Like so:

' Look for a proxy address first
Dim _ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

' If there is no proxy, get the standard remote address
If (_ip = "" Or _ip.ToLower = "unknown") Then _
    _ip = Request.ServerVariables("REMOTE_ADDR")

Some more samples:

C#

// Look for a proxy address first
    String _ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

// If there is no proxy, get the standard remote address
If (_ip == "" || _ip.ToLower == "unknown")
    _ip = Request.ServerVariables["REMOTE_ADDR"];

ASP/VBScript

ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if ipaddress = "" then
   ipaddress = Request.ServerVariables("REMOTE_ADDR")
end if
norcal johnny
  • 2,050
  • 2
  • 13
  • 17