views:

53

answers:

2

trying to fetch IP address using this:-

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

This code gives output as 127.0.0.1

and the code below displays nothing!

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

Hpw do I fetch the IP address ? the REAL IP address of a user ??

[EDIT]

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

+1  A: 
Request.Params["REMOTE_ADDR"]
J Angwenyi
this too is displaying 127.0.0.1 as output
Serenity
@happysoul perhaps because you are testing on localhost ?
driis
+1  A: 

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 ());
          }
Sachin Shanbhag