Question

I am trying to fetch the IP address using this:-

    protected void Page_Load(object sender, EventArgs e)
    {
        string ClientIP;
        ClientIP = HttpContext.Current.Request.UserHostAddress;
        Label1.Text = ClientIP;
    }

This code outputs 127.0.0.1.

And the code below displays nothing!

    string ClientIP;
    ClientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    Label1.Text = ClientIP;

How do I fetch the IP address? The REAL IP address of a user?

[EDIT]

I don't want EXACT location BTW. I need to know the country and then redirect the user to a webpage accordingly.

Was it helpful?

Solution

Request.Params["REMOTE_ADDR"]

OTHER TIPS

Using System.Net, try this -

// Then using host name, get the IP address list..
          IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
          IPAddress [] addr = ipEntry.AddressList;

          for (int i = 0; i < addr.Length; i++)
          {
              Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
          }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top