核心中的NTLM身份验证HttpClient

时间:2017-12-14 22:09:54

标签: c# httpclient core ntlm

我正在尝试使用HttpClient来访问需要NTLM身份验证的REST服务。但是我一直在获得401 Unauthorized。 我的代码看起来像这样

    private static void Main()
    {
        var uri = new Uri("http://localhost:15001");
        var credentialsCache = new CredentialCache { { uri, "NTLM", CredentialCache.DefaultNetworkCredentials } };
        var handler = new HttpClientHandler { Credentials = credentialsCache };
        var httpClient = new HttpClient(handler) { BaseAddress = uri, Timeout = new TimeSpan(0, 0, 10) };
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var response = httpClient.GetAsync("api/MyMethod").Result;
    }

我的目标框架是netcoreapp2.0。如果我改为net461,它将起作用。不确定我做错了什么?

1 个答案:

答案 0 :(得分:2)

微软已将此视为错误。核心2.1

可能会发布修复程序

https://github.com/dotnet/corefx/issues/25988