获取客户端Ip地址方法

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;
}

上一篇:Jenkins integration for AngularJS code coverage


下一篇:jquery+ajax跨域请求webservice