.NET Core 中的 HttpClientHandler UseDefaultCredentials

HttpClientHandler UseDefaultCredentials in .NET Core

.NET Framework 4.7.2 控制台应用程序中使用以下 运行 并且 some_url 指向我的 Windows 中的服务器使用 Kerberos 的网络,我得到 200 的 HTTP 代码返回:

var handler = new HttpClientHandler { UseDefaultCredentials = true };
var client = new HttpClient(handler);

var code = client.GetAsync(some_url).Result.StatusCode

但是在 .NET Core 3.1 应用程序中使用相同的代码会得到 401。这与我在 .NET Framework 4.7.2 中 运行 但未将 UseDefaultCredentials 设置为 true 时得到的相同,让我相信.NET Core 3.1 版本不传递凭据。

我需要做什么才能让 .NET Core 3.1 应用程序通过当前凭据? 我试过将 handler 上的 Credentials 设置为 NetworkCredential 的实例,但无济于事。

您应该将 AllowAutoRedirect 更改为 false 并循环响应消息

HttpResponseMessage httpResponse;
do
{
   httpResponse = await httpClient.GetAsync(uri);
   if (httpResponse.Headers.Contains("Location"))
   {
       //Find the redirection address
        uri = httpResponse.Headers.Location.OriginalString;
   }
   else
   {
       throw new ApplicationException("No Location header found in web response");
   }
 } while (httpResponse.StatusCode == HttpStatusCode.Redirect);