Ejemplo n.º 1
0
        // CheckNonSecuredEndpointForRedirect
        //   This function does an unauthenticated GET request to a non-https
        //   URL to see if the server will respond with a 302 redirect. If so, it will
        //   attempt to use the value of the Location header to perform Autodiscover.
        //
        // Parameters:
        //   useSoapEndpoints: Indicates whether SOAP endpoints should be tried.
        //
        // Returns:
        //   A Dictionary object that contains the settings returned by the
        //   Autodiscover process. If null, either the redirect failed or the URL failed.
        //
        private Dictionary <string, string> CheckNonSecuredEndpointForRedirect(bool useSoapEndpoints)
        {
            // Get the non-https URL, or "last ditch" URL.
            string nonHttpsEndpoint = UrlList.GetLastDitchUrl(EmailAddress);

            Tracing.WriteLine("Sending non-authenticated GET to " + nonHttpsEndpoint);

            HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(nonHttpsEndpoint);

            getRequest.Method            = "GET";
            getRequest.AllowAutoRedirect = false;
            getRequest.PreAuthenticate   = false;

            try
            {
                HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
                if (getResponse != null)
                {
                    if (getResponse.StatusCode == HttpStatusCode.Redirect ||
                        getResponse.StatusCode == HttpStatusCode.Moved ||
                        getResponse.StatusCode == HttpStatusCode.RedirectKeepVerb ||
                        getResponse.StatusCode == HttpStatusCode.RedirectMethod)
                    {
                        Tracing.WriteLine("Received a redirect status: " + getResponse.StatusCode.ToString());
                        string redirectUrl = getResponse.Headers["Location"].ToString();
                        Tracing.WriteLine("Location header: " +
                                          (string.IsNullOrEmpty(redirectUrl) ? "MISSING" : redirectUrl));
                        if (IsValidRedirectUrl(redirectUrl))
                        {
                            // You got a valid redirect; try it.
                            UrlRedirects++;
                            return(TryAutodiscoverUrl(AutodiscoverUrlList.NormalizeAutodiscoverUrl(redirectUrl),
                                                      useSoapEndpoints));
                        }
                        else
                        {
                            Tracing.WriteLine("Redirect returned missing or invalid URL, unable to proceed.");
                        }
                    }
                    else
                    {
                        Tracing.WriteLine("Received a non-redirect status: " + getResponse.StatusCode.ToString());
                    }
                }
            }
            catch (WebException e)
            {
                Tracing.WriteLine("Unable to connect.");
                Tracing.WriteLine(e.ToString());
            }

            return(null);
        }