Пример #1
0
        public static async Task <AuthenticationContainer> RequestAuthenticationInformationUpdateAsync(AuthenticationMode authenticationMode, Uri instanceUri, ILoginRequestHandler loginRequestHandler)
        {
            try
            {
                switch (authenticationMode)
                {
                case AuthenticationMode.NoneOrBasic:
                    return(AuthenticationContainer.FromUsernamePasswordCredential(
                               await loginRequestHandler.CredentialRequestAsync(instanceUri).ConfigureAwait(false)));

                case AuthenticationMode.Windows:
                    return(new AuthenticationContainer(AuthenticationMode.Windows));

                case AuthenticationMode.Certificate:
                case AuthenticationMode.HardwareCertificate:
                    return(AuthenticationContainer.FromCertificateCredential(
                               await loginRequestHandler.CertificateRequestAsync(instanceUri, authenticationMode == AuthenticationMode.HardwareCertificate)
                               .ConfigureAwait(false)));

                case AuthenticationMode.OAuth:
                    return(AuthenticationContainer.FromOAuthTokenCredential(
                               await loginRequestHandler.OAuthRequestAsync(instanceUri).ConfigureAwait(false)));

                default:
                    throw new ArgumentOutOfRangeException(nameof(authenticationMode), authenticationMode, null);
                }
            }
            catch (OperationCanceledException ex)
            {
                throw new LoginCanceledException("Login canceled", ex);
            }
        }
Пример #2
0
        public static bool IsAuthenticationContainerIncomplete(AuthenticationContainer authenticationContainer)
        {
            switch (authenticationContainer.Mode)
            {
            case AuthenticationMode.Certificate:
            case AuthenticationMode.HardwareCertificate:
                return(authenticationContainer.Certificate == null);

            case AuthenticationMode.OAuth:
                return(authenticationContainer.OAuthAccessToken == null);
            }

            return(false);
        }
Пример #3
0
        private async Task <TResult> PerformRequestAsync <TResult>(
            Func <HttpRequestMessage> requestCreationHandler,
            bool streamed, Func <HttpResponseMessage, Task <TResult> > handler = null,
            bool autoDisposeResponse            = true,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            HttpRequestMessage  request  = null;
            HttpResponseMessage response = null;

            try
            {
                await CheckAuthenticationContainerAsync().ConfigureAwait(false);

                var completionOptions = streamed ? HttpCompletionOption.ResponseHeadersRead : HttpCompletionOption.ResponseContentRead;

                while (true)
                {
                    request = requestCreationHandler();
                    SetDefaultHttpHeaders(request);
                    response = await _HttpClient.SendAsync(request, completionOptions, cancellationToken).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        if (handler != null)
                        {
                            return(await handler(response).ConfigureAwait(false));
                        }

                        return(default(TResult));
                    }

                    AuthenticationContainer updatedAuthentication = null;

                    updatedAuthentication = await UpdateAuthenticationInformationAsync(response).ConfigureAwait(false);

                    if (updatedAuthentication != null)
                    {
                        response.Dispose();

                        AuthenticationContainer = updatedAuthentication;
                    }
                    else
                    {
                        await HandleFaultedResponse(response).ConfigureAwait(false);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                throw new RestClientException($"Error fetching web service response for request [{request?.RequestUri}]: {ex.Message}", ex);
            }
            catch (TaskCanceledException ex) when(!cancellationToken.IsCancellationRequested)
            {
                // we expect the TaskCanceledException to be a timeout if the passed token has not been canceled
                throw new TimeoutException("Timeout reached", ex);
            }
            finally
            {
                if (autoDisposeResponse)
                {
                    response?.Dispose();
                }
            }
        }