HttpClient,UseDefaultCredentials,Windows身份验证,.NET Core 2.0+控制台应用程序收到401未经授权

编程入门 行业动态 更新时间:2024-10-28 00:27:51
本文介绍了HttpClient,UseDefaultCredentials,Windows身份验证,.NET Core 2.0+控制台应用程序收到401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从.NET Core 3.0控制台应用程序进行HTTP GET调用。端点需要Windows身份验证。终结点是IIS上的一个生产中的.NET Framework 4.5.2 Web API服务,已成功被多个客户端应用程序使用。

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 Web API service on IIS successfully used by several client applications.

我的测试程序成功运行 net45 框架目标。相比之下, netcoreapp3.0 内部版本收到401未经授权。

My test program succeeds for a net45 framework target. In contrast, the netcoreapp3.0 build receives a 401 Unauthorized.

这是我的方法:

using System; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; static async Task<string> Get(string serverUri, string requestUri) { var handler = new HttpClientHandler() { UseDefaultCredentials = true }; var client = new HttpClient(handler) { BaseAddress = new Uri(serverUri) }; return await client.GetStringAsync(requestUri); }

这是我的项目文件。

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworks>net45;netcoreapp3.0</TargetFrameworks> </PropertyGroup> <ItemGroup Condition="'$(TargetFramework)' == 'net45'"> <PackageReference Include="System.Net.Http" Version="4.3" /> </ItemGroup> </Project>

运行 。\bin\Debug\net45\HttpClientTest。 exe 返回预期的JSON结果。

Running .\bin\Debug\net45\HttpClientTest.exe returns the expected JSON result.

运行 dotnet .\bin\Debug\netcoreapp3.0\ \HttpClientTest.dll 收到401未经授权。

Running dotnet .\bin\Debug\netcoreapp3.0\HttpClientTest.dll receives a 401 Unauthorized.

Unhandled exception. System.AggregateException: One or more errors occurred. (Response status code does not indicate success: 401 (Unauthorized).) ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask) at ConsoleApp1.Program.Get(String serverUri, String requestUri) in C:\temp\coreapp3\Program.cs:line 16 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task`1.get_Result() at ConsoleApp1.Program.Main(String[] args) in C:\temp\coreapp3\Program.cs:line 22

我该如何解决?

我也尝试了以下变体,但输出没有任何变化:

I have also tried the variations below, without any change in output:

handler.Credentials = CredentialCache.DefaultNetworkCredentials; handler.Credentials = CredentialCache.DefaultCredentials; handler.Credentials = new NetworkCredential(username, password, domain);

推荐答案

此变通办法适用于我,在创建HttpClient /之前将其添加处理程序:

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

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

更多推荐

HttpClient,UseDefaultCredentials,Windows身份验证,.NET Core 2.0+控制台应用程序收到401未经授权

本文发布于:2023-11-12 19:17:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   未经授权   应用程序   身份验证   UseDefaultCredentials

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!