private bool TrySetBasicAuthToken(HttpRequestMessage request) { NetworkCredential credential = _credentials.GetCredential(request.RequestUri, Basic); if (credential == null) { return(false); } request.Headers.Authorization = new AuthenticationHeaderValue(Basic, BasicAuthenticationHelper.GetBasicTokenForCredential(credential)); return(true); }
private async Task <HttpResponseMessage> SendWithProxyAsync( Uri proxyUri, HttpRequestMessage request, CancellationToken cancellationToken) { if (proxyUri.Scheme != UriScheme.Http) { throw new InvalidOperationException($"invalid scheme {proxyUri.Scheme} for proxy"); } if (request.RequestUri.Scheme == UriScheme.Https) { // TODO #21452: Implement SSL tunneling through proxy throw new NotImplementedException("no support for SSL tunneling through proxy"); } HttpConnection connection = await GetOrCreateConnection(request, proxyUri).ConfigureAwait(false); HttpResponseMessage response = await connection.SendAsync(request, cancellationToken).ConfigureAwait(false); // Handle proxy authentication if (response.StatusCode == HttpStatusCode.ProxyAuthenticationRequired) { foreach (AuthenticationHeaderValue h in response.Headers.ProxyAuthenticate) { // We only support Basic auth, ignore others const string Basic = "Basic"; if (h.Scheme == Basic) { NetworkCredential credential = _proxy.Credentials?.GetCredential(proxyUri, Basic) ?? _defaultCredentials?.GetCredential(proxyUri, Basic); if (credential != null) { response.Dispose(); request.Headers.ProxyAuthorization = new AuthenticationHeaderValue(Basic, BasicAuthenticationHelper.GetBasicTokenForCredential(credential)); connection = await GetOrCreateConnection(request, proxyUri).ConfigureAwait(false); response = await connection.SendAsync(request, cancellationToken).ConfigureAwait(false); } break; } } } return(response); }