使用Request.servervariables c#时获得奇怪的ip

本文关键字:ip Request servervariables 使用 | 更新日期: 2023-09-27 18:21:03

我的web应用程序在本地IIS服务器上运行。调用我的web应用程序的api时使用fiddler我得到一个奇怪的客户ip地址。

public static class HttpRequestMessageHelper
{
    public static string GetClientIpAddress(this HttpRequestMessage request)
    {
        string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ip))
        {
            ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            if (string.IsNullOrEmpty(ip))
            {
                return "Unknown IP-Adress";
            }
        }
        return ip;
    }
}

我正在使用这种扩展方法来获取ip。我得到的ip看起来是这样的:"fe80::745a:d3fa:db2c:7b94%11"

使用Request.servervariables c#时获得奇怪的ip

这是一个IPv6地址:http://en.wikipedia.org/wiki/IPv6

由于全球都在运行IPv4地址(xxx.xxx.xxx.xxx),新标准是引入更多的地址。

遗憾的是,它们不那么容易记住,所以DNS查找将是至关重要的IMHO。

你可以检查你的适配器设置/ipconfig和/或DHCP服务器(也许是本地路由器?),看看你是否有可能获得IPv6地址。