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);
Wallace kelly
source share