我正在从 ASP.NET 获取客户端 IP 地址。但是某些客户端 IP 地址收到 127.0.0.1。有什么问题。如何获取有效的客户端 IP 地址?
我正在使用这段代码:
public static string GetIP()
{
string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(clientIp))
{
string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
clientIp = forwardedIps[forwardedIps.Length - 1];
}
if (string.IsNullOrEmpty(clientIp))
clientIp = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
if (string.IsNullOrEmpty(clientIp))
clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return clientIp.ToString();
}