本文介绍了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未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:54