本文介绍了HttpClient、UseDefaultCredentials、Windows 身份验证、.NET Core 2.0+ 控制台应用程序收到 401 Unauthorized的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 .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 Unauthorized.

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>

运行 .inDebuget45HttpClientTest.exe 返回预期的 JSON 结果.

Running .inDebuget45HttpClientTest.exe returns the expected JSON result.

运行 dotnet .inDebugetcoreapp3.0HttpClientTest.dll 收到 401 Unauthorized.

Running dotnet .inDebugetcoreapp3.0HttpClientTest.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:	empcoreapp3Program.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:	empcoreapp3Program.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/Handler 之前添加此:

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

07-23 18:54