Search code examples
c#asp.netrequest.servervariablesremote-host

ASP.NET question regarding getting the IP address of a user


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.


Solution

  • Request.Params["REMOTE_ADDR"]