Esempio n. 1
0
 private void TrySetCredentialsAndProxy(WebRequest request)
 {
     // Used the cached credentials and proxy we have
     request.Credentials = CredentialStore.Instance.GetCredentials(Uri);
     request.Proxy       = ProxyCache.Instance.GetProxy(Uri);
     STSAuthHelper.PrepareSTSRequest(request);
 }
Esempio n. 2
0
 private static void SetCredentialsAndProxy(WebRequest request)
 {
     if (request.Credentials == null)
     {
         request.Credentials = CredentialStore.Instance.GetCredentials(request.RequestUri);
     }
     if (request.Proxy == null)
     {
         request.Proxy = ProxyCache.Instance.GetProxy(request.RequestUri);
     }
     STSAuthHelper.PrepareSTSRequest(request);
 }
Esempio n. 3
0
        private void ConfigureRequest(HttpWebRequest request)
        {
            request.Proxy = _proxyCache.GetProxy(request.RequestUri);
            if (request.Proxy != null && request.Proxy.Credentials == null)
            {
                request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }

            if (_previousResponse == null || ShouldKeepAliveBeUsedInRequest(_previousRequest, _previousResponse))
            {
                if (_previousStatusCode == HttpStatusCode.ProxyAuthenticationRequired && _previousResponse == null &&
                    EnvironmentUtility.IsMonoRuntime)
                {
                    GetProxyCredentials(request);
                }
                else
                {
                    // Try to use the cached credentials (if any, for the first request)
                    request.Credentials = _credentialCache.GetCredentials(request.RequestUri);

                    // If there are no cached credentials, use the default ones
                    if (request.Credentials == null)
                    {
                        request.UseDefaultCredentials = true;
                    }
                }
            }
            else if (_previousStatusCode == HttpStatusCode.ProxyAuthenticationRequired)
            {
                GetProxyCredentials(request);
            }
            else if (_previousStatusCode == HttpStatusCode.Unauthorized)
            {
                SetCredentialsOnAuthorizationError(request);
            }

            SetKeepAliveHeaders(request, _previousResponse);
            if (_usingSTSAuth)
            {
                // Add request headers if the server requires STS based auth.
                STSAuthHelper.PrepareSTSRequest(request);
            }

            // Wrap the credentials in a CredentialCache in case there is a redirect
            // and credentials need to be kept around.
            request.Credentials = request.Credentials.AsCredentialCache(request.RequestUri);
        }
Esempio n. 4
0
 private void ConfigureRequest(HttpWebRequest request)
 {
     request.Proxy = this._proxyCache.GetProxy(request.RequestUri);
     if ((request.Proxy != null) && (request.Proxy.Credentials == null))
     {
         request.Proxy.Credentials = CredentialCache.DefaultCredentials;
     }
     if ((this._previousResponse == null) || ShouldKeepAliveBeUsedInRequest(this._previousRequest, this._previousResponse))
     {
         request.Credentials = this._credentialCache.GetCredentials(request.RequestUri);
         if (request.Credentials == null)
         {
             request.UseDefaultCredentials = true;
         }
     }
     else
     {
         HttpStatusCode?nullable = this._previousStatusCode;
         HttpStatusCode proxyAuthenticationRequired = HttpStatusCode.ProxyAuthenticationRequired;
         if ((((HttpStatusCode)nullable.GetValueOrDefault()) == proxyAuthenticationRequired) ? (nullable != null) : false)
         {
             request.Proxy.Credentials = this._credentialProvider.GetCredentials(request, CredentialType.ProxyCredentials, this._proxyCredentialsRetryCount > 0);
             this._continueIfFailed    = request.Proxy.Credentials != null;
             this._proxyCredentialsRetryCount++;
         }
         else
         {
             nullable = this._previousStatusCode;
             proxyAuthenticationRequired = HttpStatusCode.Unauthorized;
             if ((((HttpStatusCode)nullable.GetValueOrDefault()) == proxyAuthenticationRequired) ? (nullable != null) : false)
             {
                 this.SetCredentialsOnAuthorizationError(request);
             }
         }
     }
     SetKeepAliveHeaders(request, this._previousResponse);
     if (this._usingSTSAuth)
     {
         STSAuthHelper.PrepareSTSRequest(request);
     }
     request.Credentials = request.Credentials.AsCredentialCache(request.RequestUri);
 }
Esempio n. 5
0
        internal static WebResponse GetResponse(Func <WebRequest> createRequest,
                                                Action <WebRequest> prepareRequest,
                                                IProxyCache proxyCache,
                                                ICredentialCache credentialCache,
                                                ICredentialProvider credentialProvider)
        {
            HttpWebRequest   previousRequest    = null;
            IHttpWebResponse previousResponse   = null;
            HttpStatusCode?  previousStatusCode = null;
            bool             usingSTSAuth       = false;
            bool             continueIfFailed   = true;
            int proxyCredentialsRetryCount      = 0;
            int credentialsRetryCount           = 0;

            while (true)
            {
                // Create the request
                var request = (HttpWebRequest)createRequest();
                request.Proxy = proxyCache.GetProxy(request.RequestUri);
                if (request.Proxy != null && request.Proxy.Credentials == null)
                {
                    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                }

                if (previousResponse == null || ShouldKeepAliveBeUsedInRequest(previousRequest, previousResponse))
                {
                    // Try to use the cached credentials (if any, for the first request)
                    request.Credentials = credentialCache.GetCredentials(request.RequestUri);

                    // If there are no cached credentials, use the default ones
                    if (request.Credentials == null)
                    {
                        request.UseDefaultCredentials = true;
                    }
                }
                else if (previousStatusCode == HttpStatusCode.ProxyAuthenticationRequired)
                {
                    request.Proxy.Credentials = credentialProvider.GetCredentials(request, CredentialType.ProxyCredentials, retrying: proxyCredentialsRetryCount > 0);

                    continueIfFailed = request.Proxy.Credentials != null;

                    proxyCredentialsRetryCount++;
                }
                else if ((previousStatusCode == HttpStatusCode.Unauthorized) && !usingSTSAuth)
                {
                    // If we are using STS, the auth's being performed by a request header. We do not need to ask the user for credentials at this point.
                    request.Credentials = credentialProvider.GetCredentials(request, CredentialType.RequestCredentials, retrying: credentialsRetryCount > 0);

                    continueIfFailed = request.Credentials != null;

                    credentialsRetryCount++;
                }

                try
                {
                    ICredentials credentials = request.Credentials;

                    SetKeepAliveHeaders(request, previousResponse);

                    if (usingSTSAuth)
                    {
                        // Add request headers if the server requires STS based auth.
                        STSAuthHelper.PrepareSTSRequest(request);
                    }

                    // Prepare the request, we do something like write to the request stream
                    // which needs to happen last before the request goes out
                    prepareRequest(request);

                    // Wrap the credentials in a CredentialCache in case there is a redirect
                    // and credentials need to be kept around.
                    request.Credentials = request.Credentials.AsCredentialCache(request.RequestUri);

                    WebResponse response = request.GetResponse();

                    // Cache the proxy and credentials
                    proxyCache.Add(request.Proxy);

                    credentialCache.Add(request.RequestUri, credentials);
                    credentialCache.Add(response.ResponseUri, credentials);

                    return(response);
                }
                catch (WebException ex)
                {
                    using (IHttpWebResponse response = GetResponse(ex.Response))
                    {
                        if (response == null &&
                            ex.Status != WebExceptionStatus.SecureChannelFailure)
                        {
                            // No response, something went wrong so just rethrow
                            throw;
                        }

                        // Special case https connections that might require authentication
                        if (ex.Status == WebExceptionStatus.SecureChannelFailure)
                        {
                            if (continueIfFailed)
                            {
                                // Act like we got a 401 so that we prompt for credentials on the next request
                                previousStatusCode = HttpStatusCode.Unauthorized;
                                continue;
                            }
                            throw;
                        }

                        // If we were trying to authenticate the proxy or the request and succeeded, cache the result.
                        if (previousStatusCode == HttpStatusCode.ProxyAuthenticationRequired &&
                            response.StatusCode != HttpStatusCode.ProxyAuthenticationRequired)
                        {
                            proxyCache.Add(request.Proxy);
                        }
                        else if (previousStatusCode == HttpStatusCode.Unauthorized &&
                                 response.StatusCode != HttpStatusCode.Unauthorized)
                        {
                            credentialCache.Add(request.RequestUri, request.Credentials);
                            credentialCache.Add(response.ResponseUri, request.Credentials);
                        }

                        usingSTSAuth = STSAuthHelper.TryRetrieveSTSToken(request.RequestUri, response);

                        if (!IsAuthenticationResponse(response) || !continueIfFailed)
                        {
                            throw;
                        }

                        previousRequest    = request;
                        previousResponse   = response;
                        previousStatusCode = previousResponse.StatusCode;
                    }
                }
            }
        }