public static string GetIp()
{
string ip;
HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED_FOR"))
{
string httpXff = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(httpXff) && !httpXff.Contains("unknown"))
{
string[] xffList = httpXff.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
if (xffList.Length >= 1)
{
return xffList[0];
}
}
}
string clientIp = request.ServerVariables["HTTP_CLIENT_IP"];
if (clientIp != null)
{
ip = clientIp;
}
else
{
string remoteAddress = request.ServerVariables["REMOTE_ADDR"];
ip = remoteAddress ?? "0.0.0.0";
}
return ip;
}