コード例 #1
0
 public void Log(string message)
 {
     _logHandler.Write(message);
 }
コード例 #2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpRequestMessage clonedRequest = null;
            string             token         = null;

            // See if the request has an authorize header
            var auth = request.Headers.Authorization;

            if (auth != null)
            {
                // Authorization required! Get the token from saved settings if available
                if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Authorization required with scheme {auth.Scheme}");
                }
                token = GetToken();
                if (!string.IsNullOrWhiteSpace(token))
                {
                    // We have one, then clone the request in case we need to re-issue it with a refreshed token
                    if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                    {
                        _logHandler.Write($"Apizr - {_handlerFriendlyName}: Saved token will be used");
                    }
                    clonedRequest = await this.CloneHttpRequestMessageAsync(request);
                }
                else
                {
                    // Refresh the token
                    if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                    {
                        _logHandler.Write($"Apizr - {_handlerFriendlyName}: No token saved yet. Refreshing token...");
                    }
                    token = await this.RefreshTokenAsync(request).ConfigureAwait(false);
                }

                // Set the authentication header
                request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);
                if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Authorization header has been set");
                }
            }

            // Send the request
            if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
            {
                _logHandler.Write($"Apizr - {_handlerFriendlyName}: Sending request with authorization header...");
            }
            var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

            // Check if we get an Unauthorized response with token from settings
            if (response.StatusCode == HttpStatusCode.Unauthorized && auth != null && clonedRequest != null)
            {
                if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Unauthorized !");
                }

                // Refresh the token
                if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Refreshing token...");
                }
                token = await this.RefreshTokenAsync(request).ConfigureAwait(false);

                // Set the authentication header with refreshed token
                clonedRequest.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);
                if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Authorization header has been set with refreshed token");
                }

                // Send the request
                if (_apizrOptions.ApizrVerbosity >= ApizrLogLevel.Low)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Sending request again but with refreshed authorization header...");
                }
                response = await base.SendAsync(clonedRequest, cancellationToken).ConfigureAwait(false);
            }

            // Clear the token if unauthorized
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                token = null;
                if (_apizrOptions.ApizrVerbosity >= ApizrLogLevel.Low)
                {
                    _logHandler.Write($"Apizr - {_handlerFriendlyName}: Unauthorized ! Token has been cleared");
                }
            }

            // Save the refreshed token if succeed or clear it if not
            this.SetToken(token);
            if (_apizrOptions.ApizrVerbosity == ApizrLogLevel.High)
            {
                _logHandler.Write($"Apizr - {_handlerFriendlyName}: Token saved");
            }

            return(response);
        }