示例#1
0
        private void ConfigureProxy(HttpWebRequest request, WebStream.ProxyAuthMode mode)
        {
            IWebProxy proxy = request.Proxy;
            Uri       uri   = request.Proxy.GetProxy(request.RequestUri);

            switch (mode)
            {
            case WebStream.ProxyAuthMode.None:
            {
                request.Proxy = new WebStream.ForcedCredentialWebProxy(proxy, null);
                return;
            }

            case WebStream.ProxyAuthMode.Kerberos:
            {
                CredentialCache credentialCaches = new CredentialCache()
                {
                    { uri, "kerberos", proxy.Credentials.GetCredential(uri, "kerberos") }
                };
                request.Proxy           = new WebStream.ForcedCredentialWebProxy(proxy, credentialCaches);
                request.PreAuthenticate = true;
                return;
            }

            case WebStream.ProxyAuthMode.Ntlm:
            {
                CredentialCache credentialCaches1 = new CredentialCache()
                {
                    { uri, "ntlm", proxy.Credentials.GetCredential(uri, "ntlm") }
                };
                request.Proxy = new WebStream.ForcedCredentialWebProxy(proxy, credentialCaches1);
                return;
            }

            case WebStream.ProxyAuthMode.Other:
            {
                CredentialCache credentialCaches2 = new CredentialCache()
                {
                    { uri, "basic", proxy.Credentials.GetCredential(uri, "basic") },
                    { uri, "digest", proxy.Credentials.GetCredential(uri, "digest") }
                };
                request.Proxy = new WebStream.ForcedCredentialWebProxy(proxy, credentialCaches2);
                return;
            }

            default:
            {
                return;
            }
            }
        }
示例#2
0
        private void PreauthorizeUpStreamRequestIfNeeded(Uri endpointLocation, WebStream.ProxyAuthMode authMode)
        {
            switch (authMode)
            {
            case WebStream.ProxyAuthMode.Kerberos:
            case WebStream.ProxyAuthMode.Ntlm:
            {
                try
                {
                    HttpWebRequest requestCachePolicy = (HttpWebRequest)WebRequest.Create(endpointLocation);
                    requestCachePolicy.Method = "HEAD";
                    requestCachePolicy.ConnectionGroupName       = this.connectionGroupId;
                    requestCachePolicy.Timeout                   = -1;
                    requestCachePolicy.CachePolicy               = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                    requestCachePolicy.AllowWriteStreamBuffering = false;
                    requestCachePolicy.Headers.Add("X-WSCONNECT", this.webStreamRole);
                    requestCachePolicy.Headers.Add("X-PROCESS-AT", "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/roles/relay");
                    if (requestCachePolicy.Proxy != null)
                    {
                        this.ConfigureProxy(requestCachePolicy, authMode);
                    }
                    this.ConfigurePreauthorizationRequest(requestCachePolicy, authMode);
                    requestCachePolicy.ServicePoint.UseNagleAlgorithm = false;
                    requestCachePolicy.ServicePoint.Expect100Continue = false;
                    requestCachePolicy.GetResponse();
                }
                catch (WebException webException)
                {
                }
                return;
            }

            default:
            {
                return;
            }
            }
        }
示例#3
0
        private void ConfigurePreauthorizationRequest(HttpWebRequest request, WebStream.ProxyAuthMode authMode)
        {
            switch (authMode)
            {
            case WebStream.ProxyAuthMode.Kerberos:
            {
                request.PreAuthenticate = true;
                request.UnsafeAuthenticatedConnectionSharing = true;
                return;
            }

            case WebStream.ProxyAuthMode.Ntlm:
            {
                request.UnsafeAuthenticatedConnectionSharing = true;
                return;
            }

            default:
            {
                return;
            }
            }
        }
示例#4
0
        private void CreateSession(out Uri endpointLocation1, out Uri endpointLocation2)
        {
            WebStream.ProxyAuthMode[] proxyAuthModeArray = new WebStream.ProxyAuthMode[] { WebStream.ProxyAuthMode.None, WebStream.ProxyAuthMode.Kerberos, WebStream.ProxyAuthMode.Ntlm, WebStream.ProxyAuthMode.Other };
            this.proxyAuthMode = WebStream.ProxyAuthMode.None;
            string empty = string.Empty;

            WebStream.ProxyAuthMode[] proxyAuthModeArray1 = proxyAuthModeArray;
            int num = 0;

Label1:
            while (num < (int)proxyAuthModeArray1.Length)
            {
                WebStream.ProxyAuthMode proxyAuthMode = proxyAuthModeArray1[num];
                switch (proxyAuthMode)
                {
                case WebStream.ProxyAuthMode.Kerberos:
                {
                    if (empty.Contains("KERBEROS"))
                    {
                        break;
                    }
                    goto Label0;
                }

                case WebStream.ProxyAuthMode.Ntlm:
                {
                    if (!empty.Contains("NTLM"))
                    {
                        goto Label0;
                    }
                    else
                    {
                        break;
                    }
                }
                }
                try
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.factoryEndpointUri);
                    httpWebRequest.KeepAlive   = false;
                    httpWebRequest.Method      = "POST";
                    httpWebRequest.ContentType = "text/plain";
                    httpWebRequest.Headers.Add("X-WSCREATE", this.webStreamRole);
                    httpWebRequest.Headers.Add("X-PROCESS-AT", "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/roles/relay");
                    if (httpWebRequest.Proxy != null)
                    {
                        this.ConfigureProxy(httpWebRequest, proxyAuthMode);
                    }
                    httpWebRequest.ServicePoint.ConnectionLimit = 2048;
                    using (Stream requestStream = httpWebRequest.GetRequestStream())
                    {
                        using (StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.UTF8))
                        {
                            streamWriter.Write(string.Empty);
                            streamWriter.Flush();
                        }
                    }
                    HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
                    if (response.StatusCode != HttpStatusCode.Created || response.Headers["X-WSENDPT1"] == null || response.Headers["X-WSENDPT2"] == null)
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.FaultyEndpointResponse), null);
                    }
                    if (!Uri.TryCreate(response.Headers["X-WSENDPT1"], UriKind.Absolute, out endpointLocation1))
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.URIEndpoint), null);
                    }
                    if (!Uri.TryCreate(response.Headers["X-WSENDPT2"], UriKind.Absolute, out endpointLocation2))
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.URIEndpoint), null);
                    }
                    response.Close();
                    this.proxyAuthMode = proxyAuthMode;
                }
                catch (WebException webException1)
                {
                    WebException    webException    = webException1;
                    HttpWebResponse httpWebResponse = webException.Response as HttpWebResponse;
                    if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.ProxyAuthenticationRequired)
                    {
                        string str = WebStream.CloseResponseInWebException(webException);
                        throw Fx.Exception.AsWarning(new CommunicationException(string.Concat(SRClient.FactoryEndpoint, str), webException), null);
                    }
                    empty = httpWebResponse.Headers["Proxy-Authenticate"].ToUpperInvariant();
                    goto Label0;
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    if (!Fx.IsFatal(exception))
                    {
                        throw Fx.Exception.AsError(new CommunicationException(SRClient.FactoryEndpoint, exception), null);
                    }
                    throw;
                }
                return;
            }
            ExceptionTrace exceptionTrace   = Fx.Exception;
            CultureInfo    invariantCulture = CultureInfo.InvariantCulture;

            object[] objArray = new object[] { empty };
            throw exceptionTrace.AsError(new CommunicationException(string.Format(invariantCulture, "Failed to authenticate with proxy supporting modes '{0}'.", objArray)), null);
Label0:
            num++;
            goto Label1;
        }