HttpClient, UseDefaultCredentials, Windows Authentication, .NET Core 2.0+ console application receives 401 Unauthorized

前端 未结 2 1554
情书的邮戳
情书的邮戳 2021-02-12 14:11

I am trying to make an HTTP GET call from a .NET Core 3.0 console application. The endpoint expects Windows Authentication. The endpoint is an in-production .NET Framework 4.5.2

相关标签:
2条回答
  • 2021-02-12 14:41

    This workaround works for me add this before creating HttpClient / Handler:

    AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
    
    0 讨论(0)
  • 2021-02-12 14:55

    So we are running into the same issue with HttpClient, but on the first request it is unauthorized, but if we do a another quick request call, it goes through just fine, and we get authorized.

    If I do this on a .NET 4.7.1 console app, it works perfect. If I do it in a .NET 4.7.1 empty web app, it works fine, only if I do .NET Core 2.x do we get this issue.

    var result = string.Empty;
    var responseMessage = _client.GetAsync(url).Result;
    
    if (responseMessage.IsSuccessStatusCode)
    {
        result = await responseMessage.Content.ReadAsStringAsync();
    }
    else
    {
        var responseMessage2 = _client.GetAsync(url).Result;
    
        if (responseMessage2.IsSuccessStatusCode)
        {
            result = await responseMessage2.Content.ReadAsStringAsync();
        }
    }
    
    return result;
    
    0 讨论(0)
提交回复
热议问题