Get the IP address in ASP.NET

To get the ip address of client machine in asp.net use the following code snippet.

private string GetUserIP()
{
    string ip = Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"];
    string trueIP = string.Empty;
    if (!string.IsNullOrEmpty(ip))
    {
        trueIP = ip;
    }
    else
    {
        ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(ip))
        {
            string[] ipRange = ip.Split(',');
            trueIP = ipRange[0];
        }
        else
        {
            trueIP = Request.ServerVariables["REMOTE_ADDR"];
        }
    }


    return trueIP;
}
zp8497586rq
zp8497586rq
Tags: , , ,

Comments are closed.