Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core MVC 6 ASP.NET 5的IPAddress null_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net core MVC 6 ASP.NET 5的IPAddress null

Asp.net core MVC 6 ASP.NET 5的IPAddress null,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,如何在ASP.NET 5/MVC 6 vNext中提取IP地址? 我用的是RC1 这不起作用,在非本地主机地址上返回null if (httpContext.Connection.RemoteIpAddress != null) { currentIp = httpContext.Connection.RemoteIpAddress.ToString(); } 这是可行的,但很混乱: private static string GetIPAddress(HttpRequest

如何在ASP.NET 5/MVC 6 vNext中提取IP地址? 我用的是RC1

这不起作用,在非本地主机地址上返回null

if (httpContext.Connection.RemoteIpAddress != null)
{
     currentIp = httpContext.Connection.RemoteIpAddress.ToString();
}
这是可行的,但很混乱:

    private static string GetIPAddress(HttpRequest httpRequest)
    {
        string keys = string.Empty;
        foreach (string key in httpRequest.Headers.Keys)
        {
            keys = keys + key + "#";
        }

        string[] arrKeys = keys.Split('#');

        int x = 0;
        foreach (string value in httpRequest.Headers.Values)
        {
            if (arrKeys[x].Trim() == "X-Forwarded-For")
            {
                string[] arrIP = value.Split(':');
                if (arrIP[0] != null)
                {
                    // ip found
                    return arrIP[0];
                }
            }
            x++;
        }

        // no ip found
        return string.Empty;
    }

由于存在一个错误,无法正确解析正确的头文件,因此此操作当前无法按预期工作。你可以从他们的回购协议中追踪

在他们解决该问题之前,您必须使用变通方法