private IWebProxy GetProxyInternal(Uri uri) { WebProxy result = null; WebProxy systemProxy = GetSystemProxy(uri); if (_proxyCache.ContainsKey(systemProxy.Address)) { return(_proxyCache[systemProxy.Address]); } // Try and see if we have credentials saved for the system proxy first so that we can // validate and see if we should use them. if (_credentialProvider.HasCredentials(systemProxy.Address)) { WebProxy savedCredentialsProxy = GetSystemProxy(uri); savedCredentialsProxy.Credentials = _credentialProvider.GetCredentials(systemProxy.Address).FirstOrDefault(); if (IsProxyValid(savedCredentialsProxy, uri)) { result = savedCredentialsProxy; } } // If we did not find any saved credentials then let's try to use Default Credentials which is // used for Integrated Authentication if (null == result) { WebProxy integratedAuthProxy = GetSystemProxy(uri); integratedAuthProxy.Credentials = _credentialProvider.DefaultCredentials; if (IsProxyValid(integratedAuthProxy, uri)) { result = integratedAuthProxy; } } // If we did not succeed in getting a proxy by this time then let's try and prompt the user for // credentials and do that until we have succeeded. if (null == result) { WebProxy noCredentialsProxy = GetSystemProxy(uri); bool validCredentials = false; bool retryCredentials = false; ICredentials basicCredentials = null; while (!validCredentials) { // Get credentials for the proxy address and not the target url // because we'll end up prompting the user for a proxy for each different // package due to the packages having different urls. basicCredentials = _credentialProvider.PromptUserForCredentials(systemProxy.Address, retryCredentials); // If the provider returned credentials that are null that means the user cancelled the prompt // and we want to stop at this point and return nothing. if (null == basicCredentials) { result = null; retryCredentials = false; break; } noCredentialsProxy.Credentials = basicCredentials; if (IsProxyValid(noCredentialsProxy, uri)) { validCredentials = true; } else { retryCredentials = true; validCredentials = false; } } result = noCredentialsProxy; } Debug.Assert(null != result, "Proxy should not be null here."); if (null != result) { _proxyCache.Add(systemProxy.Address, result); } return(result); }