sledgemapper/Sledgemapper/AuthenticatedHttpClientHandler.cs
Michele Scandura 333c6c4046
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
stability improvements
2021-09-22 16:16:46 +01:00

38 lines
1.3 KiB
C#

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper
{
internal class AuthenticatedHttpClientHandler : HttpClientHandler
{
private readonly Func<Task<string>> _getToken;
public AuthenticatedHttpClientHandler(Func<Task<string>> getToken)
{
_getToken = getToken ?? throw new ArgumentNullException(nameof(getToken));
//if (myConfigurationService.VerifySslCertificate == false)
//{
ServerCertificateCustomValidationCallback =
(message, certificate, chain, sslPolicyErrors) => true;
//}
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
// See if the request has an authorize header
var auth = request.Headers.Authorization;
if (auth != null)
{
var token = await _getToken().ConfigureAwait(false);
request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);
}
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
}