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); }
protected internal override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { Uri proxyUri = null; try { if (!_proxy.IsBypassed(request.RequestUri)) { proxyUri = _proxy.GetProxy(request.RequestUri); } } catch (Exception) { // Eat any exception from the IWebProxy and just treat it as no proxy. // TODO: This seems a bit questionable, but it's what the tests expect } if (proxyUri == null) { return(await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false)); } if (proxyUri.Scheme != "http") { throw new InvalidOperationException($"invalid scheme {proxyUri.Scheme} for proxy"); } if (request.RequestUri.Scheme == "https") { // TODO: 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 && _proxy.Credentials != null) { foreach (AuthenticationHeaderValue h in response.Headers.ProxyAuthenticate) { // We only support Basic auth, ignore others if (h.Scheme == "Basic") { NetworkCredential credential = _proxy.Credentials.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); }
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); }