在共享点Web部件中获取用户的IP地址,但不提供有效的IP地址

时间:2013-07-22 19:10:55

标签: c# sharepoint-2010 web-parts

此标准自定义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,但是当我从其他计算机访问时,它会按预期工作:/

1 个答案:

答案 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