internal static bool GetAuthenticatedAutodiscoverEndpointFromHttpWebResponse(HttpWebResponse response, int redirectCount, out string endpoint)
 {
     endpoint = string.Empty;
     try
     {
         using (Stream responseStream = response.GetResponseStream())
         {
             using (StreamReader streamReader = new StreamReader(responseStream))
             {
                 XmlDocument xmlDocument = new XmlDocument();
                 xmlDocument.LoadXml(streamReader.ReadToEnd());
                 using (XmlNodeList xmlNodeList = xmlDocument.SelectNodes("AutodiscoverResponse/Root/Link"))
                 {
                     if (xmlNodeList.Count == 0)
                     {
                         return(false);
                     }
                     string redirectUrl = LyncAutodiscoverWorker.GetRedirectUrl(xmlNodeList);
                     if (!string.IsNullOrEmpty(redirectUrl))
                     {
                         endpoint = LyncAutodiscoverWorker.ExecuteAnonymousLyncAutodiscoverRedirect(redirectUrl, redirectCount + 1);
                     }
                     else
                     {
                         endpoint = LyncAutodiscoverWorker.GetTargetAttributeValue(xmlNodeList, "href", "token", "OAuth");
                     }
                 }
             }
         }
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
        internal static string ExecuteAnonymousLyncAutodiscoverRequests(bool sendInternal, string domain, string user)
        {
            LyncAutodiscoverWorker.details.AppendLine(string.Format("Executing: ExecuteAnonymousLyncAutodiscoverRequests", new object[0]));
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(LyncAutodiscoverWorker.CertificateValidationCallBack);
            string result = string.Empty;
            string arg    = string.Format("?sipuri={0}", user);

            try
            {
                string text  = sendInternal ? string.Format("http://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscoverinternal", domain, arg) : string.Format("http://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscover", domain, arg);
                string text2 = sendInternal ? string.Format("https://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscoverinternal", domain, arg) : string.Format("https://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscover", domain, arg);
                LyncAutodiscoverWorker.details.AppendLine(string.Format("HttpUrl:{0}", text));
                LyncAutodiscoverWorker.details.AppendLine(string.Format("HttpsUrl:{0}", text2));
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text);
                httpWebRequest.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState.Request = httpWebRequest;
                HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(text2);
                httpWebRequest2.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState2 = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState2.Request = httpWebRequest2;
                LyncAutodiscoverWorker.allDone.Reset();
                IAsyncResult asyncResult  = httpWebRequest.BeginGetResponse(new AsyncCallback(LyncAutodiscoverWorker.ProcessLyncAnonymousAutodiscoverResponse), lyncAutodiscoverRequestState);
                IAsyncResult asyncResult2 = httpWebRequest2.BeginGetResponse(new AsyncCallback(LyncAutodiscoverWorker.ProcessLyncAnonymousAutodiscoverResponse), lyncAutodiscoverRequestState2);
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(LyncAutodiscoverWorker.TimeoutCallback), httpWebRequest, 120000, true);
                ThreadPool.RegisterWaitForSingleObject(asyncResult2.AsyncWaitHandle, new WaitOrTimerCallback(LyncAutodiscoverWorker.TimeoutCallback), httpWebRequest2, 120000, true);
                LyncAutodiscoverWorker.allDone.WaitOne();
                if (lyncAutodiscoverRequestState.Response != null)
                {
                    lyncAutodiscoverRequestState.Response.Close();
                }
                if (lyncAutodiscoverRequestState2.Response != null)
                {
                    lyncAutodiscoverRequestState2.Response.Close();
                }
                if (string.IsNullOrEmpty(lyncAutodiscoverRequestState.TargetUrl) && string.IsNullOrEmpty(lyncAutodiscoverRequestState2.TargetUrl))
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Both http and https responses are empty.", new object[0]));
                    return(result);
                }
                if (!string.IsNullOrEmpty(lyncAutodiscoverRequestState.TargetUrl))
                {
                    if (lyncAutodiscoverRequestState.IsRedirect)
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("Redirecting to {0}.", lyncAutodiscoverRequestState.TargetUrl));
                        result = LyncAutodiscoverWorker.ExecuteAnonymousLyncAutodiscoverRedirect(lyncAutodiscoverRequestState.TargetUrl, 0);
                    }
                    else
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("Authenticated Endpoint: {0}.", lyncAutodiscoverRequestState.TargetUrl));
                        result = lyncAutodiscoverRequestState.TargetUrl;
                    }
                }
                else if (lyncAutodiscoverRequestState2.IsRedirect)
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Redirecting to {0}.", lyncAutodiscoverRequestState.TargetUrl));
                    result = LyncAutodiscoverWorker.ExecuteAnonymousLyncAutodiscoverRedirect(lyncAutodiscoverRequestState.TargetUrl, 0);
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Authenticated Endpoint: {0}.", lyncAutodiscoverRequestState.TargetUrl));
                    result = lyncAutodiscoverRequestState2.TargetUrl;
                }
            }
            catch (WebException ex)
            {
                LyncAutodiscoverWorker.details.AppendLine(string.Format("Exception: {0}.", ex.ToString()));
            }
            return(result);
        }