#region 获得客户端IP
/// <summary>
/// 获得客户端IP
/// </summary>
/// <returns></returns>
public string GetClientIp()
{
var ip = "127.0.0.1";
//穿过代理服务器取远程用户真实IP地址
if (HttpContext.Current == null
|| HttpContext.Current.Request == null
|| HttpContext.Current.Request.ServerVariables == null)
{
}
//CDN加速后取到的IP
else if (HttpContext.Current.Request.Headers["Cdn-Src-Ip"] != null)
{
ip = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
}
else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else if (HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
}
else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
ip = HttpContext.Current.Request.UserHostAddress;
}
return ip;
}
#endregion 获得客户端IP