public HttpSourceAuthenticationHandler( PackageSource packageSource, HttpClientHandler clientHandler, ICredentialService credentialService) : base(clientHandler) { _packageSource = packageSource ?? throw new ArgumentNullException(nameof(packageSource)); _clientHandler = clientHandler ?? throw new ArgumentNullException(nameof(clientHandler)); // credential service is optional as credentials may be attached to a package source _credentialService = credentialService; // Create a new wrapper for ICredentials that can be modified if (_credentialService == null || !_credentialService.HandlesDefaultCredentials) { // This is used to match the value of HttpClientHandler.UseDefaultCredentials = true _credentials = new HttpSourceCredentials(CredentialCache.DefaultNetworkCredentials); } else { _credentials = new HttpSourceCredentials(); } if (packageSource.Credentials != null && packageSource.Credentials.IsValid()) { _credentials.Credentials = packageSource.Credentials.ToICredentials(); } _clientHandler.Credentials = _credentials; // Always take the credentials from the helper. _clientHandler.UseDefaultCredentials = false; }
public NuGetHttpSourceAuthenticationHandler( PackageSource packageSource, IHttpCredentialsHandler credentialsHandler, ICredentialService credentialService) { if (packageSource == null) { throw new ArgumentNullException(nameof(packageSource)); } this.packageSource = packageSource; if (credentialsHandler == null) { throw new ArgumentNullException(nameof(credentialsHandler)); } this.credentialsHandler = credentialsHandler; // credential service is optional as credentials may be attached to a package source this.credentialService = credentialService; // Create a new wrapper for ICredentials that can be modified if (credentialService == null || !credentialService.HandlesDefaultCredentials) { // This is used to match the value of HttpClientHandler.UseDefaultCredentials = true credentials = new HttpSourceCredentials(CredentialCache.DefaultNetworkCredentials); } else { credentials = new HttpSourceCredentials(); } if (packageSource.Credentials != null && packageSource.Credentials.IsValid()) { var sourceCredentials = new NetworkCredential(packageSource.Credentials.Username, packageSource.Credentials.Password); credentials.Credentials = sourceCredentials; } this.credentialsHandler.Credentials = credentials; // Always take the credentials from the helper. this.credentialsHandler.UseDefaultCredentials = false; }