internal static string GetUcwaDiscoveryUrl(string userAddress, ICredentials credentials)
        {
            int num = userAddress.LastIndexOf("@");

            if (num <= 0)
            {
                return(string.Empty);
            }
            string domain = userAddress.Substring(num + 1);
            LyncAnonymousAutodiscoverResult authenticatedAutodiscoverEndpoint = LyncAutodiscoverWorker.GetAuthenticatedAutodiscoverEndpoint(userAddress, domain);
            string authenticatedServerUri = authenticatedAutodiscoverEndpoint.AuthenticatedServerUri;

            if (string.IsNullOrEmpty(authenticatedServerUri.Trim()) || authenticatedServerUri.Length == 0)
            {
                return(string.Empty);
            }
            LyncAutodiscoverResult ucwaUrl = LyncAutodiscoverWorker.GetUcwaUrl(authenticatedAutodiscoverEndpoint.AuthenticatedServerUri, credentials);

            return(ucwaUrl.UcwaDiscoveryUrl);
        }
        internal static LyncAutodiscoverResult GetUcwaUrl(string authenticatedEndpointUrl, ICredentials credentials)
        {
            LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrl");
            LyncAutodiscoverResult lyncAutodiscoverResult = new LyncAutodiscoverResult();

            lyncAutodiscoverResult.IsUcwaSupported  = false;
            lyncAutodiscoverResult.UcwaDiscoveryUrl = string.Empty;
            string text = string.Empty;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(LyncAutodiscoverWorker.CertificateValidationCallBack);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(authenticatedEndpointUrl);
                httpWebRequest.Accept      = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                httpWebRequest.Timeout     = 120000;
                httpWebRequest.Credentials = credentials;
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState.Request = httpWebRequest;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse.ToString();
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine("Response is NULL");
                }
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrlFromHttpWebResponse to get ucwa URL.");
                    if (LyncAutodiscoverWorker.GetUcwaUrlFromHttpWebResponse(httpWebResponse, credentials, 0, out text))
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("ucwa URL: {0}", text));
                        lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
                        return(lyncAutodiscoverResult);
                    }
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("unable to find ucwa URL from response.", new object[0]));
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Http status code: {0}", httpWebResponse.StatusCode));
                }
            }
            catch (WebException ex)
            {
                LyncAutodiscoverWorker.details.AppendLine(string.Format("Exception: {0}", ex.ToString()));
                HttpWebResponse httpWebResponse2 = ex.Response as HttpWebResponse;
                if (httpWebResponse2 != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse2.ToString();
                    HttpStatusCode statusCode = httpWebResponse2.StatusCode;
                    if (statusCode == HttpStatusCode.Unauthorized)
                    {
                        text = ((ex.InnerException == null) ? ex.Message : ex.InnerException.Message);
                    }
                }
            }
            lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
            lyncAutodiscoverResult.IsUcwaSupported  = !string.IsNullOrWhiteSpace(text);
            lyncAutodiscoverResult.DiagnosticInfo   = LyncAutodiscoverWorker.details.ToString();
            LyncAutodiscoverWorker.details.Clear();
            return(lyncAutodiscoverResult);
        }
示例#3
0
        public static ResultType SendLyncOAuthRequest(ADUser user, Uri targetUri, out string diagnosticMessage, bool appOnly = false, bool useCachedToken = false, bool reloadConfig = false)
        {
            int              startIndex       = user.GetFederatedIdentity().Identity.IndexOf('@') + 1;
            string           domain           = user.GetFederatedIdentity().Identity.Substring(startIndex);
            StringBuilder    stringBuilder    = new StringBuilder();
            ICredentials     icredentials     = TestOAuthConnectivityHelper.GetICredentials(appOnly, user, domain);
            OAuthCredentials oauthCredentials = icredentials as OAuthCredentials;
            string           text             = string.Empty;

            foreach (ProxyAddress proxyAddress in user.EmailAddresses)
            {
                if (proxyAddress.ToString().Contains("sip:"))
                {
                    text = TestOAuthConnectivityHelper.FromSipFormat(proxyAddress.ToString());
                }
            }
            if (string.IsNullOrEmpty(text))
            {
                text = user.PrimarySmtpAddress.ToString();
            }
            if (string.IsNullOrEmpty(text))
            {
                diagnosticMessage = Strings.EMailAddressNotFound(user.Alias);
                return(ResultType.Error);
            }
            Guid value = Guid.NewGuid();

            oauthCredentials.ClientRequestId = new Guid?(value);
            stringBuilder.AppendLine(Strings.ClientRequestId(value.ToString()));
            string value2 = TestOAuthConnectivityHelper.CheckReloadConfig(reloadConfig);
            string value3 = TestOAuthConnectivityHelper.CheckUseCachedToken(useCachedToken);

            stringBuilder.AppendLine(value2);
            stringBuilder.AppendLine(value3);
            ValidationResultCollector resultCollector    = new ValidationResultCollector();
            LocalConfiguration        localConfiguration = LocalConfiguration.Load(resultCollector);

            oauthCredentials.Tracer             = new TestOAuthConnectivityHelper.TaskOauthOutboundTracer();
            oauthCredentials.LocalConfiguration = localConfiguration;
            LyncAnonymousAutodiscoverResult lyncAnonymousAutodiscoverResult = null;

            try
            {
                lyncAnonymousAutodiscoverResult = LyncAutodiscoverWorker.GetAuthenticatedAutodiscoverEndpoint(text, domain);
            }
            catch (WebException ex)
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.AutodiscoverFailure);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                stringBuilder.AppendLine(ex.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            catch (Exception ex2)
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.AutodiscoverFailure);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                stringBuilder.AppendLine(ex2.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            if (string.IsNullOrEmpty(lyncAnonymousAutodiscoverResult.AuthenticatedServerUri))
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(Strings.NoAuthenticatedServerUri);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            stringBuilder.AppendLine(Strings.DiagnosticsHeader);
            stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
            LyncAutodiscoverResult lyncAutodiscoverResult = null;

            try
            {
                lyncAutodiscoverResult = LyncAutodiscoverWorker.GetUcwaUrl(lyncAnonymousAutodiscoverResult.AuthenticatedServerUri, icredentials);
            }
            catch (WebException ex3)
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.UCWADiscoveryUrlException);
                stringBuilder.AppendLine(lyncAutodiscoverResult.Response);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                stringBuilder.AppendLine(ex3.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            catch (Exception ex4)
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.UCWADiscoveryUrlException);
                stringBuilder.AppendLine(lyncAutodiscoverResult.Response);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                stringBuilder.AppendLine(ex4.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            if (string.IsNullOrEmpty(lyncAutodiscoverResult.UcwaDiscoveryUrl))
            {
                stringBuilder.AppendLine(Strings.DiagnosticsHeader);
                stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
                stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
                stringBuilder.AppendLine(Strings.UCWADiscoveryUrlEmpty);
                stringBuilder.AppendLine(lyncAutodiscoverResult.Response);
                stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
                diagnosticMessage = stringBuilder.ToString();
                return(ResultType.Error);
            }
            stringBuilder.AppendLine(Strings.DiagnosticsHeader);
            stringBuilder.AppendLine(lyncAnonymousAutodiscoverResult.DiagnosticInfo);
            stringBuilder.AppendLine(Strings.TestOutboundOauthLog);
            stringBuilder.AppendLine(oauthCredentials.Tracer.ToString());
            diagnosticMessage = stringBuilder.ToString();
            return(ResultType.Success);
        }