Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
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;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the ExchangeService from a given row.
        /// </summary>
        private ExchangeService LoadExchangeServiceFromProfileRow(
            ServicesProfile.ServiceBindingRow row,
            NetworkCredential cred,
            out EWSEditor.Common.EwsEditorAppSettings oAppSettings)
        {
            // Create the ExchangeService


            if (!row.IsRequestedServerVersionNull())
            {
                // Convert the string to an ExchangeVersion enumeration
                ExchangeVersion req =
                    (ExchangeVersion)System.Enum.Parse(typeof(ExchangeVersion), row.RequestedServerVersion);
                EwsProxyFactory.RequestedExchangeVersion = req;
            }


            // Load autodiscover email if found
            if (!row.IsAutoDiscoverEmailNull())
            {
                EwsProxyFactory.AllowAutodiscoverRedirect = GlobalSettings.AllowAutodiscoverRedirect;
                EwsProxyFactory.ServiceEmailAddress       = new EmailAddress(row.AutoDiscoverEmail);
            }

            if (!row.IsServicesURLNull())
            {
                EwsProxyFactory.EwsUrl = new Uri(row.ServicesURL);
            }


            // If ImpersonationType is specified then we assume an Id is as well
            if (!row.IsImpersonationTypeNull())
            {
                // Convert the string to a ConnectingIdType enumeration
                ConnectingIdType impType =
                    (ConnectingIdType)System.Enum.Parse(typeof(ConnectingIdType), row.ImpersonationType);
                EwsProxyFactory.UserToImpersonate = new ImpersonatedUserId(impType, row.ImpersonationId);
            }

            // If credentials are not required then use DefaultCredentials
            if (row.UsesDefaultCredentials)
            {
                EwsProxyFactory.UseDefaultCredentials = true;
            }
            else
            {
                if (cred != null)
                {
                    EwsProxyFactory.ServiceCredential = cred;
                }
                else
                {
                    throw new ApplicationException("Credentials are required for this binding.");
                }
            }


            ExchangeService oService = EwsProxyFactory.CreateExchangeService();

            oAppSettings = new EWSEditor.Common.EwsEditorAppSettings();
            EwsProxyFactory.SetAppSettingsFromProxyFactory(ref oAppSettings);

            return(oService);
            //return EwsProxyFactory.CreateExchangeService();
        }