/// <summary>
        /// Determines if the authentication module can handle the challenge sent with the response.
        /// </summary>
        /// <param name="client">The REST client the response is assigned to</param>
        /// <param name="request">The REST request the response is assigned to</param>
        /// <param name="credentials">The credentials to be used for the authentication</param>
        /// <param name="response">The response that returned the authentication challenge</param>
        /// <returns>true when the authenticator can handle the sent challenge</returns>
        public virtual bool CanHandleChallenge(IHttpClient client, IHttpRequestMessage request, ICredentials credentials, IHttpResponseMessage response)
        {
            // No credentials defined?
            if (credentials == null)
            {
                return(false);
            }

            // No challenge header found?
            var authModeInfo = response.GetAuthenticationMethodValue(_authHeader, AuthenticationMethod);

            if (authModeInfo == null)
            {
                return(false);
            }

            // Search for credential for request URI
            var responseUri = client.GetRequestUri(request, response);
            var credential  = credentials.GetCredential(responseUri, AuthenticationMethod);

            if (credential == null)
            {
                return(false);
            }

            // Did we already try to use the found credentials?
            if (ReferenceEquals(credential, _authCredential))
            {
                // Yes, so we don't retry the authentication.
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Will be called when the authentication failed
        /// </summary>
        /// <param name="client">Client executing this request</param>
        /// <param name="request">Request to authenticate</param>
        /// <param name="credentials">The credentials used for the authentication</param>
        /// <param name="response">Response of the failed request</param>
        /// <returns>Task where the handler for a failed authentication gets executed</returns>
        public Task HandleChallenge(IHttpClient client, IHttpRequestMessage request, ICredentials credentials, IHttpResponseMessage response)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (!CanHandleChallenge(client, request, credentials, response))
                {
                    throw new InvalidOperationException();
                }

                var responseUri = client.GetRequestUri(request, response);
                _authCredential = credentials.GetCredential(responseUri, AuthenticationMethod);
                var authModeInfo = response.GetAuthenticationMethodValue(_authHeader, AuthenticationMethod);
                ParseResponseHeader(authModeInfo);
            }));
        }
        /// <summary>
        /// Will be called when the authentication failed
        /// </summary>
        /// <param name="client">Client executing this request</param>
        /// <param name="request">Request to authenticate</param>
        /// <param name="credentials">The credentials used for the authentication</param>
        /// <param name="response">Response of the failed request</param>
        /// <returns>Task where the handler for a failed authentication gets executed</returns>
        public Task HandleChallenge(IHttpClient client, IHttpRequestMessage request, ICredentials credentials, IHttpResponseMessage response)
        {
            if (!CanHandleChallenge(client, request, credentials, response))
            {
                throw new InvalidOperationException();
            }

            var responseUri = client.GetRequestUri(request, response);

            _authCredential = credentials.GetCredential(responseUri, AuthenticationMethod);
            var authModeInfo = response.GetAuthenticationMethodValue(_authHeader, AuthenticationMethod);

            ParseResponseHeader(authModeInfo);

#if USE_TASKEX
            return(TaskEx.FromResult(0));
#else
            return(Task.FromResult(0));
#endif
        }
        /// <summary>
        /// Will be called when the authentication failed
        /// </summary>
        /// <param name="client">Client executing this request</param>
        /// <param name="request">Request to authenticate</param>
        /// <param name="credentials">The credentials used for the authentication</param>
        /// <param name="response">Response of the failed request</param>
        /// <returns>Task where the handler for a failed authentication gets executed</returns>
        public Task HandleChallenge(IHttpClient client, IHttpRequestMessage request, ICredentials credentials, IHttpResponseMessage response)
        {
            return Task.Factory.StartNew(() =>
            {
                if (!CanHandleChallenge(client, request, credentials, response))
                    throw new InvalidOperationException();

                var responseUri = client.GetRequestUri(request, response);
                _authCredential = credentials.GetCredential(responseUri, AuthenticationMethod);
                var authModeInfo = response.GetAuthenticationMethodValue(_authHeader, AuthenticationMethod);
                ParseResponseHeader(authModeInfo);
            });
        }
        /// <summary>
        /// Determines if the authentication module can handle the challenge sent with the response.
        /// </summary>
        /// <param name="client">The REST client the response is assigned to</param>
        /// <param name="request">The REST request the response is assigned to</param>
        /// <param name="credentials">The credentials to be used for the authentication</param>
        /// <param name="response">The response that returned the authentication challenge</param>
        /// <returns>true when the authenticator can handle the sent challenge</returns>
        public virtual bool CanHandleChallenge(IHttpClient client, IHttpRequestMessage request, ICredentials credentials, IHttpResponseMessage response)
        {
            // No credentials defined?
            if (credentials == null)
                return false;

            // No challenge header found?
            var authModeInfo = response.GetAuthenticationMethodValue(_authHeader, AuthenticationMethod);
            if (authModeInfo == null)
                return false;

            // Search for credential for request URI
            var responseUri = client.GetRequestUri(request, response);
            var credential = credentials.GetCredential(responseUri, AuthenticationMethod);
            if (credential == null)
                return false;

            // Did we already try to use the found credentials?
            if (ReferenceEquals(credential, _authCredential))
            {
                // Yes, so we don't retry the authentication.
                return false;
            }

            return true;
        }