简体   繁体   English

.NET Core 中的 HttpClientHandler UseDefaultCredentials

[英]HttpClientHandler UseDefaultCredentials in .NET Core

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:通过在.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

but using the same code in a .NET Core 3.1 application gives a 401 instead.但是在.NET Core 3.1应用程序中使用相同的代码会产生401 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 Framework 4.7.2 中运行但未将UseDefaultCredentials设置为true时得到的结果相同,这让我相信.NET Core 3.1版本不会传递凭据。

What do I need to do to get a .NET Core 3.1 application to pass the current credentials?我需要做什么才能让.NET Core 3.1应用程序传递当前凭据? I've tried playing around with setting the Credentials on the handler to an instance of NetworkCredential but to no avail.我试过将handler上的Credentials设置为NetworkCredential的实例,但无济于事。

you should change AllowAutoRedirect to false and loop over the response message您应该将 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 .NET Core 中使用 HttpClientHandler 和 HttpClientFactory - How to use HttpClientHandler with HttpClientFactory in .NET Core ASP.NET 带有自定义 HttpClientHandler 的核心模拟 HttpClient - ASP.NET Core mock HttpClient with custom HttpClientHandler “HttpClientHandler”不包含“ServerCertificateValidationCallback”.Net Standard 2.0 的定义 - 'HttpClientHandler' does not contain a definition for 'ServerCertificateValidationCallback' .Net Standard 2.0 “HttpClientHandler”在 net461 和 net47 中不包含“ClientCertificates”的定义? - 'HttpClientHandler' does not contain a definition for 'ClientCertificates' in net461 and net47? NetworkCredential UseDefaultCredentials不起作用 - NetworkCredential UseDefaultCredentials not working 得到的价值<defaultProxy useDefaultCredentials= /> - Get the value of <defaultProxy useDefaultCredentials= /> 无法从方法`System.Net.ServicePointManager:CloseConnectionGroup(string)访问HttpClientHandler:Dispose(bool)&#39; - HttpClientHandler:Dispose (bool)' is inaccessible from method `System.Net.ServicePointManager:CloseConnectionGroup (string) SmtpClient UseDefaultCredentials属性的奇怪行为 - Strange behavior of SmtpClient UseDefaultCredentials property 为什么 SmtpClient.UseDefaultCredentials 被忽略? - Why SmtpClient.UseDefaultCredentials is ignored? Exchange Web服务:UseDefaultCredentials属性 - Exchange Web Services: UseDefaultCredentials property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM