alır Bir .NET Core 2.0 konsol uygulamasından bir HTTP GET çağrısı yapmaya çalışıyorum. Uç nokta Windows Kimlik Doğrulaması bekliyor.HttpClient, UseDefaultCredentials, Windows Kimlik Doğrulaması, .NET Core 2.0 konsol uygulaması 401 Yetkisiz
Test programım net45
çerçeve hedefi için başarılı. Buna karşılık, netcoreapp2.0
yapı bir 401 yetkisiz alır.
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);
}
İşte benim proje dosyasıdır:
İşte benim yöntemidir. .\bin\Debug\net45\HttpClientTest.exe
Running
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp2.0'">
<PackageReference Include="System.Net.Http" Version="4.3" />
</ItemGroup>
</Project>
beklenen JSON sonuç döndürür.
Çalışan dotnet .\bin\Debug\netcoreapp2.0\HttpClientTest.dll
, 401 Yetkisizdir.
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>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at HttpClientTest.Program.<Get>d__1.MoveNext() in C:\temp\HttpClientTest\Program.cs:line 24
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at HttpClientTest.Program.Main(String[] args) in C:\temp\HttpClientTest\Program.cs:line 16
Bunu nasıl düzeltirim?
Ben de çıktıda herhangi bir değişiklik olmadan, aşağıda varyasyonları denedim:
handler.Credentials = CredentialCache.DefaultNetworkCredentials;
handler.Credentials = CredentialCache.DefaultCredentials;
handler.Credentials = new NetworkCredential(username, password, domain);