문제

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.

도움이 되었습니까?

해결책

Request.Params["REMOTE_ADDR"]

다른 팁

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 ());
          }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top