Use default credentials on http client handler (#414)

MSAL integrated Windows authentication requires using default
credentials. This is done if using MSALs http client factory, so must
explicitly opt into this behavior.

---------

Co-authored-by: John Erickson <jerick@microsoft.com>
This commit is contained in:
John Schmeichel 2023-05-30 13:01:47 -07:00 коммит произвёл GitHub
Родитель d332e325a2
Коммит 705303f0a2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 4 добавлений и 8 удалений

Просмотреть файл

@ -2,7 +2,6 @@
//
// Licensed under the MIT license.
using System;
using System.Net.Http;
using Microsoft.Artifacts.Authentication;
using IAdalHttpClientFactory = Microsoft.IdentityModel.Clients.ActiveDirectory.IHttpClientFactory;
@ -21,15 +20,12 @@ namespace NuGetCredentialProvider.Util
static HttpClientFactory()
{
// https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines
#if NETFRAMEWORK
var httpClient = new HttpClient();
#else
var httpClient = new HttpClient(new SocketsHttpHandler
var httpClient = new HttpClient(new HttpClientHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(15)
// This is needed to make IWA work. See:
// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/e15befb5f0a6cf4757c5abcaa6487e28e7ebd1bb/src/client/Microsoft.Identity.Client/PlatformsCommon/Shared/SimpleHttpClientFactory.cs#LL26C1-L27C73
UseDefaultCredentials = true
});
#endif
httpClientFactory = new(httpClient);
}