HttpClient, UseDefaultCredentials, Windows Authentication, .NET Core 2 Console Application. 0+ gets 401 unauthorized

I am trying to make an HTTP GET call from a .NET Core 3.0 console application. The endpoint is waiting for Windows authentication. The endpoint is the IIS Web API.NET Framework 4.5.2 production service, successfully used by several client applications.

My test program succeeded for the net45 target platform. In contrast, the netcoreapp3.0 build gets 401 Unauthorized.

Here is my method:

 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); } 

Here is my project file.

 <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> 

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

Running dotnet .\bin\Debug\netcoreapp3.0\HttpClientTest.dll gets 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 

How can I fix this?

I also tried the options below, without any changes to the output:

 handler.Credentials = CredentialCache.DefaultNetworkCredentials; handler.Credentials = CredentialCache.DefaultCredentials; handler.Credentials = new NetworkCredential(username, password, domain); 
+12
source share
1 answer

Thus, we are faced with the same problem with HttpClient, but on the first request it is unauthorized use, but if we make another call to a quick request, everything will be fine, and we will get permission.

If I do this in a .NET 4.7.1 console application, it works fine. If I do this in an empty .NET 4.7.1 web application, it works fine only if I do .NET Core 2.x, we get this problem.

 var result = string.Empty; var responseMessage = _client.GetAsync(url).Result; if (responseMessage.IsSuccessStatusCode) { result = await responseMessage.Content.ReadAsStringAsync(); } else { var responseMessage2 = _client.GetAsync(url).Result; if (responseMessage2.IsSuccessStatusCode) { result = await responseMessage2.Content.ReadAsStringAsync(); } } return result; 
-2
source

All Articles