问题描述
在 .NET Framework 4.7.2 控制台应用程序中运行以下代码,并且 some_url
指向我的 Windows 网络中使用 Kerberos 的服务器,我得到了一个 HTTP 代码200 返回:
With the following running in a .NET Framework 4.7.2 Console application and some_url
pointing to a server in my Windows network that uses Kerberos, I get a HTTP code of 200 back:
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 版本不传递凭据.
but using the same code in a .NET Core 3.1 application gives a 401 instead. This is the same as I get when running in .NET Framework 4.7.2 but not setting UseDefaultCredentials
to true
, leading me to believe the .NET Core 3.1 version does not pass the credentials along.
我需要做什么才能让 .NET Core 3.1 应用程序传递当前凭据?我尝试将 handler
上的 Credentials
设置为 NetworkCredential
的实例,但无济于事.
What do I need to do to get a .NET Core 3.1 application to pass the current credentials?I've tried playing around with setting the Credentials
on the handler
to an instance of NetworkCredential
but to no avail.
推荐答案
您应该将 AllowAutoRedirect 更改为 false 并循环响应消息
you should change AllowAutoRedirect to false and loop over the response message
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);
这篇关于.NET Core 中的 HttpClientHandler UseDefaultCredentials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!