此标准自定义WebPart中的代码:
private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
返回:"fe80::c564:7922:d873:5cf5%11"
而不是有效的IP地址。它为我在Google上找到的每种方法执行此操作,包括HttpRequest.UserHostAddress。
有没有人知道发生了什么?
编辑:出于某种原因,它在本地加载页面时会给我IPv6,但是当我从其他计算机访问时,它会按预期工作:/
答案 0 :(得分:-1)
使用此,
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
或使用
string ipAddress = Request.UserHostAddress