CreateAuthenticationHeaderFromChallenge() public method

public CreateAuthenticationHeaderFromChallenge ( HttpRequestMessage request, IEnumerable challenges ) : AuthenticationHeaderValue
request System.Net.Http.HttpRequestMessage
challenges IEnumerable
return System.Net.Http.Headers.AuthenticationHeaderValue
コード例 #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Attempt to reuse last credentials used for the same origin server  (Preauthenticate)
            if (request.Headers.Authorization == null)
            {
                request.Headers.Authorization = _credentialService.CreateAuthenticationHeaderFromRequest(request);
            }

            var response = await base.SendAsync(request, cancellationToken);

            // If request failed and challenge issued
            if (response.StatusCode == HttpStatusCode.Unauthorized && response.Headers.WwwAuthenticate.Count > 0)
            {
                // Can't automatically resend the request if it is not buffered.
                // Not sure how to detect this

                var authHeader = _credentialService.CreateAuthenticationHeaderFromChallenge(request, response.Headers.WwwAuthenticate);
                if (authHeader != null)
                {
                    var newRequest = await CopyRequest(request);

                    newRequest.Headers.Authorization = authHeader;

                    // Resend request with auth header based on challenge
                    response = await base.SendAsync(newRequest, cancellationToken);
                }
            }
            return(response);
        }
コード例 #2
0
ファイル: BasicTests.cs プロジェクト: hapikit/hapikit.net
        public void Reuse_last_used_credentials()
        {
            // Arrange
            var cache = new HttpCredentialCache
            {
                new BasicCredentials(new Uri("http://example.org"), username: "", password: "")
            };

            var authService = new CredentialService(cache);
            var request = new HttpRequestMessage(){RequestUri = new Uri("http://example.org")};
            authService.CreateAuthenticationHeaderFromChallenge(request, new[] { new AuthenticationHeaderValue("basic", "") });

            // Act
            var header = authService.CreateAuthenticationHeaderFromRequest(request);

            // Assert
            Assert.Equal("basic", header.Scheme);

        }