![获取客户端Ip地址方法 获取客户端Ip地址方法](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
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;
}