コード例 #1
0
        /// <summary>
        /// A passed ExchangeService can be displayed for editing or a new
        /// service will be returned after created using this dialog.
        /// </summary>
        /// <param name="service">ExchangeService to be returned or displayed</param>
        /// <returns>DialogResult indicating the user action which closed the dialog</returns>
        public static DialogResult ShowDialog(ref ExchangeService service, ref EwsEditorAppSettings oAppSettings)
        {
            ServiceDialog dialog = new ServiceDialog();

            if (service != null)
            {
                dialog.CurrentService = service;
            }

            if (oAppSettings != null)
            {
                dialog.CurrentAppSettings = oAppSettings;
            }

            DialogResult res = dialog.ShowDialog();

            if (res == DialogResult.OK)
            {
                service = dialog.CurrentService;

                oAppSettings = dialog.CurrentAppSettings;
            }

            return(res);
        }
コード例 #2
0
        private void btnAddService_Click(object sender, EventArgs e)
        {
            var service      = new ExchangeService();
            var app_settinsg = new EwsEditorAppSettings();

            using (var dlg = new ServiceDialog())
            {
                dlg.CurrentService     = new ExchangeService();
                dlg.CurrentAppSettings = new EwsEditorAppSettings();
                dlg.CheckOnOkay        = false;

                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                StoredServices.Add(new StoredService
                {
                    Name        = txtServiceName.Text,
                    Service     = dlg.CurrentService,
                    AppSettings = dlg.CurrentAppSettings
                });

                UpdateList();
            }
        }
コード例 #3
0
        public void SetServiceLabel(EwsEditorAppSettings oAppSettings)
        {
            string sInfo = string.Empty;

            if (oAppSettings != null)
            {
                if (oAppSettings != null)
                {
                    if (oAppSettings.UserImpersonationSelected == true)
                    {
                        // With Impersonation = "ServiceAccount contacting HostName as ActAsAccount"
                        sInfo = string.Format(
                            System.Globalization.CultureInfo.CurrentCulture,
                            "Service account '{0}' is contacting mailbox '{1}' via {2} as account '{3}'.",
                            oAppSettings.AccountAccessingMailbox,
                            oAppSettings.MailboxBeingAccessed,
                            oAppSettings.UrlHost,
                            oAppSettings.ImpersonatedId);
                    }
                    else
                    {
                        sInfo = string.Format(
                            System.Globalization.CultureInfo.CurrentCulture,
                            "Service account '{0}' is contacting mailbox '{1}' via {2}.",
                            oAppSettings.AccountAccessingMailbox,
                            oAppSettings.MailboxBeingAccessed,
                            oAppSettings.UrlHost);
                    }

                    this.lblExchangeService.Text = sInfo;
                }
            }

            this.lblExchangeService.Text = sInfo;
        }
コード例 #4
0
        private void BtnOK_Click(object sender, EventArgs e)
        {
            // Validation for credential input...
            if (rdoCredentialsUserSpecified.Checked && (txtUserName.Text.Length == 0 || txtPassword.Text.Length == 0))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SPECIFY_CREDS);
                return;
            }

            // Validation for Autodiscover input...

            if (this.rdoAutodiscoverEmail.Checked && String.IsNullOrEmpty(this.AutodiscoverEmailText.Text))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SERVICE_REQ);
                return;
            }

            // Validation for URL input...
            if (this.rdoServiceUrl.Checked && String.IsNullOrEmpty(this.ExchangeServiceURLText.Text))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SERVICE_REQ);
                return;
            }

            // Validation for Impersonation input...
            if (this.ImpersonationCheck.Checked && (String.IsNullOrEmpty(this.ImpersonatedIdTextBox.Text) || !this.connectingIdCombo.SelectedItem.HasValue))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_IMPERSON_REQ);
                return;
            }

            try
            {
                Cursor = System.Windows.Forms.Cursors.WaitCursor;

                EwsProxyFactory.RequestedExchangeVersion = exchangeVersionCombo.SelectedItem;

                EwsProxyFactory.OverrideTimezone   = GlobalSettings.OverrideTimezone;
                EwsProxyFactory.SelectedTimeZoneId = GlobalSettings.SelectedTimeZoneId;

                EwsProxyFactory.AllowAutodiscoverRedirect = GlobalSettings.AllowAutodiscoverRedirect;

                EwsProxyFactory.UseDefaultCredentials = this.rdoCredentialsDefaultWindows.Checked;
                if (this.rdoCredentialsDefaultWindows.Checked)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.DefaultAuth;
                }

                EwsProxyFactory.CredentialsUserSpecified = this.rdoCredentialsUserSpecified.Checked;

                if (this.rdoCredentialsUserSpecified.Checked)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.SpecifiedCredentialsAuth;
                }

                if (this.rdoCredentialsOAuth.Checked)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.oAuth;
                }

                // MailboxBeingAccessed
                switch (EwsProxyFactory.AuthenticationMethod)
                {
                case RequestedAuthType.DefaultAuth:
                    AutodiscoverEmailText.Text = UserPrincipal.Current.EmailAddress;
                    //if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                    //    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                    break;

                case RequestedAuthType.SpecifiedCredentialsAuth:
                    if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                    {
                        EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                    }
                    else
                    {
                        EwsProxyFactory.MailboxBeingAccessed = this.txtUserName.Text.Trim();
                    }
                    break;

                case RequestedAuthType.oAuth:
                    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();      // override later in ewsproxyfactory
                    break;
                }

                if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                {
                    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                }
                if (this.ImpersonationCheck.Checked) // Override
                {
                    EwsProxyFactory.MailboxBeingAccessed = ImpersonatedIdTextBox.Text.Trim();
                }


                EwsProxyFactory.UserImpersonationSelected = this.ImpersonationCheck.Checked;
                //EwsProxyFactory.UserToImpersonate = this.ImpersonatedIdTextBox.Text  // set below
                EwsProxyFactory.ImpersonationType = this.connectingIdCombo.SelectedItem.Value.ToString();
                EwsProxyFactory.ImpersonatedId    = this.ImpersonatedIdTextBox.Text.Trim();


                EwsProxyFactory.UseoAuth         = this.rdoCredentialsOAuth.Checked;
                EwsProxyFactory.oAuthRedirectUrl = this.txtOAuthRedirectUri.Text.Trim();
                EwsProxyFactory.oAuthClientId    = this.txtOAuthAppId.Text.Trim();
                EwsProxyFactory.oAuthServerName  = this.txtOAuthServerName.Text.Trim();
                EwsProxyFactory.oAuthAuthority   = this.txtOAuthAuthority.Text.Trim();
                EwsProxyFactory.oBearerToken     = string.Empty;

                EwsProxyFactory.EnableScpLookup = GlobalSettings.EnableScpLookups;
                EwsProxyFactory.PreAuthenticate = GlobalSettings.PreAuthenticate;

                EwsProxyFactory.OverrideTimeout = GlobalSettings.OverrideTimeout;
                EwsProxyFactory.Timeout         = GlobalSettings.Timeout;
                EwsProxyFactory.UserAgent       = GlobalSettings.UserAgent;

                EwsProxyFactory.UserName = this.txtUserName.Text.Trim();
                // EwsProxyFactory.Password = this.txtPassword.Text.Trim();   // Don't keep.
                EwsProxyFactory.Domain = this.txtDomain.Text.Trim();

                EwsProxyFactory.SetDefaultProxy            = GlobalSettings.SetDefaultProxy;
                EwsProxyFactory.BypassProxyForLocalAddress = GlobalSettings.BypassProxyForLocalAddress;
                EwsProxyFactory.SpecifyProxySettings       = GlobalSettings.SpecifyProxySettings;
                EwsProxyFactory.ProxyServerName            = GlobalSettings.ProxyServerName;
                EwsProxyFactory.ProxyServerPort            = GlobalSettings.ProxyServerPort;
                EwsProxyFactory.OverrideProxyCredentials   = GlobalSettings.OverrideProxyCredentials;
                EwsProxyFactory.ProxyServerUser            = GlobalSettings.ProxyServerUser;
                EwsProxyFactory.ProxyServerPassword        = GlobalSettings.ProxyServerPassword;
                EwsProxyFactory.ProxyServerDomain          = GlobalSettings.ProxyServerDomain;



                EwsProxyFactory.EwsUrl = this.rdoAutodiscoverEmail.Checked ?
                                         null : new Uri(ExchangeServiceURLText.Text.Trim());

                EwsProxyFactory.UserToImpersonate = this.ImpersonationCheck.Checked ?
                                                    new ImpersonatedUserId(this.connectingIdCombo.SelectedItem.Value, this.ImpersonatedIdTextBox.Text.Trim()) : null;

                EwsProxyFactory.SetXAnchorMailbox = this.chkSetXAnchorMailbox.Checked;
                EwsProxyFactory.XAnchorMailbox    = this.txtXAnchorMailbox.Text.Trim();

                EwsProxyFactory.SetXPublicFolderMailbox = this.chkSetXPublicFolderMailbox.Checked;
                EwsProxyFactory.XPublicFolderMailbox    = this.txtXPublicFolderMailbox.Text.Trim();



                EwsProxyFactory.EnableAdditionalHeader1 = GlobalSettings.EnableAdditionalHeader1;
                EwsProxyFactory.AdditionalHeader1       = GlobalSettings.AdditionalHeader1;
                EwsProxyFactory.AdditionalHeaderValue1  = GlobalSettings.AdditionalHeaderValue1;
                EwsProxyFactory.EnableAdditionalHeader2 = GlobalSettings.EnableAdditionalHeader2;
                EwsProxyFactory.AdditionalHeader2       = GlobalSettings.AdditionalHeader2;
                EwsProxyFactory.AdditionalHeaderValue2  = GlobalSettings.AdditionalHeaderValue2;
                EwsProxyFactory.EnableAdditionalHeader3 = GlobalSettings.EnableAdditionalHeader3;
                EwsProxyFactory.AdditionalHeader3       = GlobalSettings.AdditionalHeader3;
                EwsProxyFactory.AdditionalHeaderValue3  = GlobalSettings.AdditionalHeaderValue3;

                EwsProxyFactory.AddTimeZoneContext        = GlobalSettings.AddTimeZoneContext;
                EwsProxyFactory.SelectedTimeZoneContextId = GlobalSettings.SelectedTimeZoneContextId;


                EwsProxyFactory.ServiceEmailAddress = this.AutodiscoverEmailText.Text.Trim();
                EwsProxyFactory.UseAutoDiscover     = this.rdoAutodiscoverEmail.Checked;

                //EwsProxyFactory.ServiceCredential = rdoCredentialsUserSpecified.Checked ?
                //   new NetworkCredential(
                //       this.txtUserName.Text.Trim(),
                //       this.txtPassword.Text.Trim(),  // This will fail on passwords ending with whitespace
                //       this.txtDomain.Text.Trim()) :
                //   null;

                // ----- Set Credentials ----
                EwsProxyFactory.ServiceCredential        = null;
                EwsProxyFactory.ServiceNetworkCredential = null;

                if (rdoCredentialsDefaultWindows.Checked)
                {
                    EwsProxyFactory.ServiceCredential = (NetworkCredential)CredentialCache.DefaultCredentials;

                    EwsProxyFactory.ServiceNetworkCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
                }

                if (rdoCredentialsUserSpecified.Checked)
                {
                    NetworkCredential oNetworkCredential = new NetworkCredential(
                        this.txtUserName.Text.Trim(),
                        this.txtPassword.Text.Trim(), // This will fail on passwords ending with whitespace
                        this.txtDomain.Text.Trim());

                    EwsProxyFactory.ServiceCredential = oNetworkCredential;

                    EwsProxyFactory.ServiceNetworkCredential = oNetworkCredential;
                }


                if (this.rdoCredentialsOAuth.Checked)
                {
                    AuthenticationHelper oAH = new AuthenticationHelper();
                    string sBearerToken      = string.Empty;
                    EwsProxyFactory.ServiceCredential = oAH.Do_OAuth(ref EwsProxyFactory.MailboxBeingAccessed, ref EwsProxyFactory.AccountAccessingMailbox,
                                                                     EwsProxyFactory.oAuthAuthority, EwsProxyFactory.oAuthClientId, EwsProxyFactory.oAuthRedirectUrl, EwsProxyFactory.oAuthServerName, ref sBearerToken);

                    EwsProxyFactory.oBearerToken = sBearerToken;

                    //EwsProxyFactory.AccountAccessingMailbox
                    //EwsProxyFactory.MailboxBeingAccessed = EwsProxyFactory.AccountAccessingMailbox;
                }

                // ----    Autodiscover    ----

                if (this.rdoAutodiscoverEmail.Checked)
                {
                    EwsProxyFactory.DoAutodiscover();
                }

                // ----    New service & app settings    ----

                CurrentService = EwsProxyFactory.CreateExchangeService();


                // ----    Save settings    ----
                EWSEditor.Common.EwsEditorAppSettings oAppSettings = new EwsEditorAppSettings();
                EwsProxyFactory.SetAppSettingsFromProxyFactory(ref oAppSettings);
                CurrentAppSettings = oAppSettings;



                //CurrentAppSettings.MailboxBeingAccessed = EwsProxyFactory.AccountAccessingMailbox;
                // CurrentAppSettings

                // EwsProxyFactory.AccountAccessingMailbox

                // ----    Do a basic test to be sure that the mailbox can be reached with an EWS call   ----
                CurrentService.TestExchangeService();

                CurrentService.OnSerializeCustomSoapHeaders += m_Service_OnSerializeCustomSoapHeaders;


                DialogResult = DialogResult.OK;
            }
            finally
            {
                Cursor = System.Windows.Forms.Cursors.Default;
            }
        }
コード例 #5
0
        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (rdoCredentialsOAuth2.Checked == true)
            {
                if (txtOAuthApplicationId.Text.Trim() == string.Empty)
                {
                    ErrorDialog.ShowInfo("Applicaiton Id is required for oAuth.");
                    return;
                }
                if (txtOAuthTenantId.Text.Trim() == string.Empty)
                {
                    ErrorDialog.ShowInfo("Tenant Id is required for oAuth.");
                    return;
                }
            }


            // Doing App permissions flow?
            if (rdoCredentialsOAuth2.Checked == true && rdoCredentialsOAuthApplication.Checked)
            {
                if (txtOAuthTenantId.Text.Trim() == string.Empty)
                {
                    ErrorDialog.ShowInfo("Client Secret is required for oAuth Applicaiton permissions flow.");
                    return;
                }

                if (this.ImpersonationCheck.Checked == false || this.ImpersonatedIdTextBox.Text.Trim() == string.Empty)
                {
                    // Impersonation is required for app permissions flow
                    ErrorDialog.ShowInfo(DisplayStrings.MSG_IMPERSONATION_NEEDED_FOR_APP_PERMISSIONS_FLOW);
                    return;
                }

                // X-AnchorMailbox is required
                if (this.chkSetXAnchorMailbox.Checked == false || this.txtXAnchorMailbox.Text.Trim() == string.Empty)
                {
                    ErrorDialog.ShowInfo(DisplayStrings.MSG_XANCHORMAILBOX_FOR_APP_PERMISSIONS_FLOW);
                    return;
                }
            }
            // Validation for credential input...
            if (rdoCredentialsUserSpecified.Checked && (txtUserName.Text.Length == 0 || txtPassword.Text.Length == 0))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SPECIFY_CREDS);
                return;
            }

            // Validation for Autodiscover input...

            if (this.rdoAutodiscoverEmail.Checked && String.IsNullOrEmpty(this.AutodiscoverEmailText.Text))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SERVICE_REQ);
                return;
            }

            // Validation for URL input...
            if (this.rdoServiceUrl.Checked && String.IsNullOrEmpty(this.ExchangeServiceURLText.Text))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_SERVICE_REQ);
                return;
            }

            // Validation for Impersonation input...
            if (this.ImpersonationCheck.Checked && (String.IsNullOrEmpty(this.ImpersonatedIdTextBox.Text) || !this.connectingIdCombo.SelectedItem.HasValue))
            {
                ErrorDialog.ShowInfo(DisplayStrings.MSG_IMPERSON_REQ);
                return;
            }

            // Impersonation should always need X-AnchorMailbox header set or call will fail at any time.
            if (this.ImpersonationCheck.Checked == true && this.chkSetXAnchorMailbox.Checked == false)
            {
                ErrorDialog.ShowInfo(DisplayStrings.WARN_IMPERSONATION_NEEDS_XANCHORMAILBOX);
            }

            try
            {
                Cursor = System.Windows.Forms.Cursors.WaitCursor;

                EwsProxyFactory.RequestedExchangeVersion = exchangeVersionCombo.SelectedItem;

                EwsProxyFactory.OverrideTimezone   = GlobalSettings.OverrideTimezone;
                EwsProxyFactory.SelectedTimeZoneId = GlobalSettings.SelectedTimeZoneId;

                EwsProxyFactory.AllowAutodiscoverRedirect = GlobalSettings.AllowAutodiscoverRedirect;

                EwsProxyFactory.UseDefaultCredentials = this.rdoCredentialsDefaultWindows.Checked;
                if (this.rdoCredentialsDefaultWindows.Checked)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.DefaultAuth;
                }

                EwsProxyFactory.CredentialsUserSpecified = this.rdoCredentialsUserSpecified.Checked;

                if (this.rdoCredentialsUserSpecified.Checked == true)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.SpecifiedCredentialsAuth;
                }

                if (this.rdoCredentialsOAuth.Checked)
                {
                    EwsProxyFactory.AuthenticationMethod = RequestedAuthType.oAuth;
                }

                if (this.rdoCredentialsOAuth2.Checked)
                {
                    if (this.rdoCredentialsOAuthDelegated.Checked)
                    {
                        EwsProxyFactory.AuthenticationMethod = RequestedAuthType.oAuth2Delegate;
                    }

                    if (this.rdoCredentialsOAuthApplication.Checked)
                    {
                        EwsProxyFactory.AuthenticationMethod = RequestedAuthType.oAuth2Application;
                    }

                    if (this.rdoCredentialsOAuthCertificate.Checked)
                    {
                        EwsProxyFactory.AuthenticationMethod = RequestedAuthType.oAuth2Certificate;
                    }
                }

                // MailboxBeingAccessed
                EwsProxyFactory.MailboxBeingAccessed = string.Empty;
                switch (EwsProxyFactory.AuthenticationMethod)
                {
                case RequestedAuthType.DefaultAuth:
                    AutodiscoverEmailText.Text = UserPrincipal.Current.EmailAddress;
                    break;

                case RequestedAuthType.SpecifiedCredentialsAuth:
                    if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                    {
                        EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                    }
                    else
                    {
                        EwsProxyFactory.MailboxBeingAccessed = this.txtUserName.Text.Trim();
                    }
                    break;

                case RequestedAuthType.oAuth2Application:
                    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                    break;

                case RequestedAuthType.oAuth2Delegate:
                    if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                    {
                        EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                    }
                    break;

                default:
                    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();      // override later in ewsproxyfactory (for oAuth)
                    break;
                }

                if (this.AutodiscoverEmailText.Text.Trim().Length != 0)
                {
                    EwsProxyFactory.MailboxBeingAccessed = this.AutodiscoverEmailText.Text.Trim();
                }
                if (this.ImpersonationCheck.Checked) // Override
                {
                    EwsProxyFactory.MailboxBeingAccessed = ImpersonatedIdTextBox.Text.Trim();
                }

                if (EwsProxyFactory.MailboxBeingAccessed == string.Empty && this.chkSetXAnchorMailbox.Checked && this.txtXAnchorMailbox.Text.Trim().Length != 0)
                {
                    EwsProxyFactory.MailboxBeingAccessed = this.txtXAnchorMailbox.Text.Trim();
                }


                EwsProxyFactory.UserImpersonationSelected = this.ImpersonationCheck.Checked;
                //EwsProxyFactory.UserToImpersonate = this.ImpersonatedIdTextBox.Text  // set below
                EwsProxyFactory.ImpersonationType = this.connectingIdCombo.SelectedItem.Value.ToString();
                EwsProxyFactory.ImpersonatedId    = this.ImpersonatedIdTextBox.Text.Trim();


                EwsProxyFactory.UseoAuth         = this.rdoCredentialsOAuth.Checked;
                EwsProxyFactory.oAuthRedirectUrl = this.txtOAuthRedirectUri.Text.Trim();
                EwsProxyFactory.oAuthClientId    = this.txtOAuthAppId.Text.Trim();
                EwsProxyFactory.oAuthServerName  = this.txtOAuthServerName.Text.Trim();
                EwsProxyFactory.oAuthAuthority   = this.txtOAuthAuthority.Text.Trim();

                EwsProxyFactory.UseoAuth2           = this.rdoCredentialsOAuth2.Checked;
                EwsProxyFactory.UseOAuthDelegate    = this.rdoCredentialsOAuthDelegated.Checked;
                EwsProxyFactory.UseOAuthApplication = this.rdoCredentialsOAuthApplication.Checked;
                EwsProxyFactory.oAuthApplicationId  = this.txtOAuthApplicationId.Text.Trim();
                EwsProxyFactory.oAuthTenantId       = this.txtOAuthTenantId.Text.Trim();
                EwsProxyFactory.oAuthClientSecret   = this.txtOAuthClientSecret.Text.Trim();
                // EwsProxyFactory.oAuthCertificate = this.txtAuthCertificate.Text.Trim();
                EwsProxyFactory.oBearerToken     = string.Empty;
                EwsProxyFactory.LogSecurityToken = GlobalSettings.LogSecurityToken;


                EwsProxyFactory.EnableScpLookup = GlobalSettings.EnableScpLookups;
                EwsProxyFactory.PreAuthenticate = GlobalSettings.PreAuthenticate;

                EwsProxyFactory.OverrideTimeout = GlobalSettings.OverrideTimeout;
                EwsProxyFactory.Timeout         = GlobalSettings.Timeout;
                EwsProxyFactory.UserAgent       = GlobalSettings.UserAgent;

                EwsProxyFactory.UserName = this.txtUserName.Text.Trim();
                // EwsProxyFactory.Password = this.txtPassword.Text.Trim();   // Don't keep.
                EwsProxyFactory.Domain = this.txtDomain.Text.Trim();

                EwsProxyFactory.SetDefaultProxy            = GlobalSettings.SetDefaultProxy;
                EwsProxyFactory.BypassProxyForLocalAddress = GlobalSettings.BypassProxyForLocalAddress;
                EwsProxyFactory.SpecifyProxySettings       = GlobalSettings.SpecifyProxySettings;
                EwsProxyFactory.ProxyServerName            = GlobalSettings.ProxyServerName;
                EwsProxyFactory.ProxyServerPort            = GlobalSettings.ProxyServerPort;
                EwsProxyFactory.OverrideProxyCredentials   = GlobalSettings.OverrideProxyCredentials;
                EwsProxyFactory.ProxyServerUser            = GlobalSettings.ProxyServerUser;
                EwsProxyFactory.ProxyServerPassword        = GlobalSettings.ProxyServerPassword;
                EwsProxyFactory.ProxyServerDomain          = GlobalSettings.ProxyServerDomain;



                EwsProxyFactory.EwsUrl = this.rdoAutodiscoverEmail.Checked ?
                                         null : new Uri(ExchangeServiceURLText.Text.Trim());

                EwsProxyFactory.UserToImpersonate = this.ImpersonationCheck.Checked ?
                                                    new ImpersonatedUserId(this.connectingIdCombo.SelectedItem.Value, this.ImpersonatedIdTextBox.Text.Trim()) : null;

                EwsProxyFactory.SetXAnchorMailbox = this.chkSetXAnchorMailbox.Checked;
                EwsProxyFactory.XAnchorMailbox    = this.txtXAnchorMailbox.Text.Trim();

                EwsProxyFactory.SetXPublicFolderMailbox = this.chkSetXPublicFolderMailbox.Checked;
                EwsProxyFactory.XPublicFolderMailbox    = this.txtXPublicFolderMailbox.Text.Trim();



                EwsProxyFactory.EnableAdditionalHeader1 = GlobalSettings.EnableAdditionalHeader1;
                EwsProxyFactory.AdditionalHeader1       = GlobalSettings.AdditionalHeader1;
                EwsProxyFactory.AdditionalHeaderValue1  = GlobalSettings.AdditionalHeaderValue1;
                EwsProxyFactory.EnableAdditionalHeader2 = GlobalSettings.EnableAdditionalHeader2;
                EwsProxyFactory.AdditionalHeader2       = GlobalSettings.AdditionalHeader2;
                EwsProxyFactory.AdditionalHeaderValue2  = GlobalSettings.AdditionalHeaderValue2;
                EwsProxyFactory.EnableAdditionalHeader3 = GlobalSettings.EnableAdditionalHeader3;
                EwsProxyFactory.AdditionalHeader3       = GlobalSettings.AdditionalHeader3;
                EwsProxyFactory.AdditionalHeaderValue3  = GlobalSettings.AdditionalHeaderValue3;

                EwsProxyFactory.AddTimeZoneContext        = GlobalSettings.AddTimeZoneContext;
                EwsProxyFactory.SelectedTimeZoneContextId = GlobalSettings.SelectedTimeZoneContextId;


                EwsProxyFactory.ServiceEmailAddress = this.AutodiscoverEmailText.Text.Trim();
                EwsProxyFactory.UseAutoDiscover     = this.rdoAutodiscoverEmail.Checked;

                //EwsProxyFactory.ServiceCredential = rdoCredentialsUserSpecified.Checked ?
                //   new NetworkCredential(
                //       this.txtUserName.Text.Trim(),
                //       this.txtPassword.Text.Trim(),  // This will fail on passwords ending with whitespace
                //       this.txtDomain.Text.Trim()) :
                //   null;

                // ----- Set Credentials ----
                EwsProxyFactory.ServiceCredential        = null;
                EwsProxyFactory.ServiceNetworkCredential = null;

                if (rdoCredentialsDefaultWindows.Checked)
                {
                    EwsProxyFactory.ServiceCredential = (NetworkCredential)CredentialCache.DefaultCredentials;

                    EwsProxyFactory.ServiceNetworkCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
                }

                if (rdoCredentialsUserSpecified.Checked)
                {
                    NetworkCredential oNetworkCredential = new NetworkCredential(
                        this.txtUserName.Text.Trim(),
                        this.txtPassword.Text.Trim(), // This will fail on passwords ending with whitespace
                        this.txtDomain.Text.Trim());

                    EwsProxyFactory.ServiceCredential = oNetworkCredential;

                    EwsProxyFactory.ServiceNetworkCredential = oNetworkCredential;
                }

                // Delegagte oAuth 1
                if (this.rdoCredentialsOAuth.Checked)
                {
                    AuthenticationHelper oAH = new AuthenticationHelper();
                    string sBearerToken      = string.Empty;
                    EwsProxyFactory.ServiceCredential = oAH.Do_OAuth(ref EwsProxyFactory.MailboxBeingAccessed, ref EwsProxyFactory.AccountAccessingMailbox,
                                                                     EwsProxyFactory.oAuthAuthority, EwsProxyFactory.oAuthClientId, EwsProxyFactory.oAuthRedirectUrl, EwsProxyFactory.oAuthServerName, ref sBearerToken);

                    EwsProxyFactory.oBearerToken = sBearerToken;
                }


                // oAuth2
                if (this.rdoCredentialsOAuth2.Checked)
                {
                    // https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth
                    // Microsoft.Identity.Client is available under NewGet
                    // https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token?tabs=dotnet

                    // https://www.skylinetechnologies.com/Blog/Skyline-Blog/December_2018/async-await-configureawait

                    // Get Access token -----------------

                    if (this.rdoCredentialsOAuthDelegated.Checked)
                    {
                        EWSEditor.Common.Auth.OAuthHelper o = new EWSEditor.Common.Auth.OAuthHelper();
                        AuthenticationResult oResult        = System.Threading.Tasks.Task.Run(async() => await o.GetDelegateToken(EwsProxyFactory.oAuthApplicationId, EwsProxyFactory.oAuthTenantId)).Result;
                        EwsProxyFactory.MsalAuthenticationResult = oResult;
                        var oCredentials = new Microsoft.Exchange.WebServices.Data.OAuthCredentials(oResult.AccessToken);

                        EwsProxyFactory.ServiceCredential              = oCredentials;
                        EwsProxyFactory.MsalAuthenticationResult       = oResult;
                        EwsProxyFactory.oBearerToken                   = oResult.AccessToken;
                        EwsProxyFactory.CurrentPublicClientApplication = o.CurrentPublicClientApplication;

                        EwsProxyFactory.MailboxBeingAccessed = oResult.Account.Username;
                    }

                    if (this.rdoCredentialsOAuthApplication.Checked)
                    {
                        EWSEditor.Common.Auth.OAuthHelper o = new EWSEditor.Common.Auth.OAuthHelper();
                        AuthenticationResult oResult        = System.Threading.Tasks.Task.Run(async() => await o.GetApplicationToken(EwsProxyFactory.oAuthApplicationId,
                                                                                                                                     EwsProxyFactory.oAuthTenantId,
                                                                                                                                     EwsProxyFactory.oAuthClientSecret)).Result;

                        var oCredentials = new Microsoft.Exchange.WebServices.Data.OAuthCredentials(oResult.AccessToken);

                        EwsProxyFactory.ServiceCredential              = oCredentials;
                        EwsProxyFactory.MsalAuthenticationResult       = oResult;
                        EwsProxyFactory.oBearerToken                   = oResult.AccessToken;
                        EwsProxyFactory.CurrentPublicClientApplication = o.CurrentPublicClientApplication;
                    }

                    if (this.rdoCredentialsOAuthCertificate.Checked)
                    {
                        EWSEditor.Common.Auth.OAuthHelper o = new EWSEditor.Common.Auth.OAuthHelper();

                        AuthenticationResult oResult = System.Threading.Tasks.Task.Run(async() => await o.GetCertificateToken(EwsProxyFactory.oAuthApplicationId,
                                                                                                                              EwsProxyFactory.oAuthTenantId,
                                                                                                                              EwsProxyFactory.oAuthClientCertificate)).Result;



                        var oCredentials = new Microsoft.Exchange.WebServices.Data.OAuthCredentials(oResult.AccessToken);

                        EwsProxyFactory.ServiceCredential              = oCredentials;
                        EwsProxyFactory.MsalAuthenticationResult       = oResult;
                        EwsProxyFactory.oBearerToken                   = oResult.AccessToken;
                        EwsProxyFactory.CurrentPublicClientApplication = o.CurrentPublicClientApplication;
                    }
                }


                // ----    Autodiscover    ----

                if (this.rdoAutodiscoverEmail.Checked)
                {
                    EwsProxyFactory.DoAutodiscover();
                }

                // ----    New service & app settings    ----

                CurrentService = EwsProxyFactory.CreateExchangeService();


                // ----    Save settings    ----
                EWSEditor.Common.EwsEditorAppSettings oAppSettings = new EwsEditorAppSettings();
                EwsProxyFactory.SetAppSettingsFromProxyFactory(ref oAppSettings);
                CurrentAppSettings = oAppSettings;


                //CurrentAppSettings.MailboxBeingAccessed = EwsProxyFactory.AccountAccessingMailbox;
                // CurrentAppSettings

                // EwsProxyFactory.AccountAccessingMailbox

                // ----    Do a basic test to be sure that the mailbox can be reached with an EWS call   ----
                CurrentService.TestExchangeService();

                CurrentService.OnSerializeCustomSoapHeaders += m_Service_OnSerializeCustomSoapHeaders;


                DialogResult = DialogResult.OK;
            }
            finally
            {
                Cursor = System.Windows.Forms.Cursors.Default;
            }
        }