lxf1117

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

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-06-29
  • 2021-08-09
  • 2021-12-31
  • 2021-12-20
  • 2022-01-17
  • 2021-07-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-06-20
相关资源
相似解决方案