Beispiel #1
0
        /// <inheritdoc/>
        protected override async Task <T> PerformRequestAsync <T>(HttpRequestMessage httpRequest, Dictionary <string, string> headers = null)
        {
            try
            {
                var authValue = await authenticator.GetAuthValue();

                httpRequest.Headers.Authorization = new AuthenticationHeaderValue(authenticator.Scheme, authValue);
            }
            catch (Exception ex)
            {
                throw new RestfulClientException("AuthClient could not obtain and/or add the authentication token", httpRequest, HttpStatusCode.Unauthorized, ex);
            }

            try
            {
                return(await base.PerformRequestAsync <T>(httpRequest, headers));
            }
            catch (RestfulClientException ex)
            {
                // If the request was unauthorized, clear the offending token from the cache to force a new token to be obtained on the next request
                if (ex.StatusCode == HttpStatusCode.Unauthorized)
                {
                    authenticator.ClearAuthValue();
                }

                throw ex;
            }
        }