Beispiel #1
0
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request.Headers.Authorization != null)
            {
                return(await base.SendAsync(request, cancellationToken));
            }

            // When the token is not fetched or it is expired, re-fetch a service account token.
            if (_token == null || _tokenExpiryDate < DateTime.UtcNow)
            {
                _token = await _account.AuthenticateAsync(_options);

                _tokenExpiryDate = DateTime.UtcNow + ServiceTokenLifetime;
            }

            request.Headers.Authorization = new("Bearer", _token);
            return(await base.SendAsync(request, cancellationToken));
        }