private static ExchangeService CreateExchangeService(string domain, EncryptionConfigurationTable.RequestData requestData)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            ADUser orgMailbox = EncryptionConfigurationTable.GetOrgMailbox(domain);

            requestData.OrgMailboxAdUserLookupTime = stopwatch.ElapsedMilliseconds;
            if (orgMailbox == null)
            {
                return(null);
            }
            ExchangePrincipal exchangePrincipal;

            try
            {
                exchangePrincipal = ExchangePrincipal.FromADUser(orgMailbox, null);
            }
            catch (ObjectNotFoundException exception)
            {
                EncryptionConfigurationTable.logger.LogEvent(ApplicationLogicEventLogConstants.Tuple_E4EOrganizationMailboxWebServiceUrlRetrievalFailed, domain, new object[]
                {
                    "ProcessEncryptionConfiguration",
                    EncryptionConfigurationTable.GetLoggedExceptionString(exception)
                });
                return(null);
            }
            Uri backEndWebServicesUrl;

            try
            {
                backEndWebServicesUrl = BackEndLocator.GetBackEndWebServicesUrl(exchangePrincipal.MailboxInfo);
                stopwatch.Stop();
                requestData.WebServiceUrlLookupTime = stopwatch.ElapsedMilliseconds - requestData.OrgMailboxAdUserLookupTime;
            }
            catch (BackEndLocatorException exception2)
            {
                EncryptionConfigurationTable.logger.LogEvent(ApplicationLogicEventLogConstants.Tuple_E4EOrganizationMailboxWebServiceUrlRetrievalFailed, domain, new object[]
                {
                    "ProcessEncryptionConfiguration",
                    EncryptionConfigurationTable.GetLoggedExceptionString(exception2)
                });
                return(null);
            }
            ExchangeService exchangeService = new ExchangeService(4);

            exchangeService.Url = backEndWebServicesUrl;
            exchangeService.PrivilegedUserId            = new PrivilegedUserId(0, 2, orgMailbox.PrimarySmtpAddress.ToString());
            exchangeService.PrivilegedUserId.BudgetType = new PrivilegedUserIdBudgetType?(2);
            RemoteCertificateValidationCallback callback = (object param0, X509Certificate param1, X509Chain param2, SslPolicyErrors param3) => true;

            CertificateValidationManager.RegisterCallback("E4E.EncryptionConfigurationTable", callback);
            exchangeService.HttpHeaders.Add(CertificateValidationManager.ComponentIdHeaderName, "E4E.EncryptionConfigurationTable");
            return(exchangeService);
        }
 internal static void SetEncryptionConfiguration(string domain, string imageBase64, string emailText, string portalText, string disclaimerText, bool otpEnabled)
 {
     EncryptionConfigurationTable.ExecuteWebServicesAction(delegate
     {
         EncryptionConfigurationTable.RequestData arg = new EncryptionConfigurationTable.RequestData();
         ExchangeService exchangeService = EncryptionConfigurationTable.CreateExchangeServiceDelegate(domain, arg);
         if (exchangeService == null)
         {
             return;
         }
         exchangeService.SetEncryptionConfiguration(imageBase64, emailText, portalText, disclaimerText, otpEnabled);
         EncryptionConfigurationData data = new EncryptionConfigurationData(imageBase64, emailText, portalText, disclaimerText, otpEnabled);
         EncryptionConfigurationTable.encryptionConfigurationDataCache.Add(domain, data);
     });
 }
        internal static EncryptionConfigurationData GetEncryptionConfiguration(string domain, bool useCache, out EncryptionConfigurationTable.RequestData requestData)
        {
            EncryptionConfigurationData encryptionConfigurationData = null;

            if (useCache && EncryptionConfigurationTable.encryptionConfigurationDataCache.TryGetValue(domain, out encryptionConfigurationData))
            {
                requestData = new EncryptionConfigurationTable.RequestData();
                return(encryptionConfigurationData);
            }
            EncryptionConfigurationTable.RequestData requestDataSave = new EncryptionConfigurationTable.RequestData();
            requestData = requestDataSave;
            EncryptionConfigurationTable.ExecuteWebServicesAction(delegate
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                ExchangeService exchangeService = EncryptionConfigurationTable.CreateExchangeServiceDelegate(domain, requestDataSave);
                if (exchangeService == null)
                {
                    return;
                }
                exchangeService.Timeout = 30000;
                requestDataSave.CreateExchangeServiceTime = stopwatch.ElapsedMilliseconds;
                GetEncryptionConfigurationResponse encryptionConfiguration = exchangeService.GetEncryptionConfiguration();
                stopwatch.Stop();
                requestDataSave.GetEncryptionConfigurationTime = stopwatch.ElapsedMilliseconds - requestDataSave.CreateExchangeServiceTime;
                requestDataSave.ExchangeServiceUri             = exchangeService.Url;
                string text;
                if (exchangeService.HttpResponseHeaders.TryGetValue("request-id", out text))
                {
                    requestDataSave.EwsRequestId = text.Replace("-", string.Empty);
                }
                else
                {
                    requestDataSave.EwsRequestId = "NotFound";
                }
                encryptionConfigurationData = new EncryptionConfigurationData(encryptionConfiguration.ImageBase64, encryptionConfiguration.EmailText, encryptionConfiguration.PortalText, encryptionConfiguration.DisclaimerText, encryptionConfiguration.OTPEnabled);
            });
            if (useCache && encryptionConfigurationData != null)
            {
                EncryptionConfigurationTable.encryptionConfigurationDataCache.Add(domain, encryptionConfigurationData);
            }
            return(encryptionConfigurationData);
        }