コード例 #1
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            /*
             *                  This is the setup process for the login control,
             *                  The login control uses a class called CrmConnectionManager to manage the interaction with CRM, this class and also be queried as later points for information about the current connection.
             *                  In this case, the login control is referred to as CrmLoginCtrl
             */

            // Set off flag.
            _bIsConnectedComplete = false;

            // Init the CRM Connection manager..
            _mgr = new CrmConnectionManager();
            // Pass a reference to the current UI or container control,  this is used to synchronize UI threads In the login control
            _mgr.ParentControl = CrmLoginCtrl;
            // if you are using an unmanaged client, excel for example, and need to store the config in the users local directory
            // set this option to true.
            _mgr.UseUserLocalDirectoryForConfigStore = true;

            if (_profileName != null)
            {
                _mgr.ProfileName = _profileName;
            }

            _mgr.ClientId    = "2ad88395-b77d-4561-9441-d0e40824f9bc";
            _mgr.RedirectUri = new Uri("app://5d3e90d6-aa8e-48a8-8f2c-58b45cc67315");
            // if you are using an unmanaged client,  you need to provide the name of an exe to use to create app config key's for.
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";
            // CrmLoginCtrl is the Login control,  this sets the CrmConnection Manager into it.
            CrmLoginCtrl.SetGlobalStoreAccess(_mgr);
            // There are several modes to the login control UI
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
            // this wires an event that is raised when the login button is pressed.
            CrmLoginCtrl.ConnectionCheckBegining += CrmLoginCtrl_ConnectionCheckBegining;
            // this wires an event that is raised when an error in the connect process occurs.
            CrmLoginCtrl.ConnectErrorEvent += CrmLoginCtrl_ConnectErrorEvent;
            // this wires an event that is raised when a status event is returned.
            CrmLoginCtrl.ConnectionStatusEvent += CrmLoginCtrl_ConnectionStatusEvent;
            // this wires an event that is raised when the user clicks the cancel button.
            CrmLoginCtrl.UserCancelClicked += CrmLoginCtrl_UserCancelClicked;
            // Check to see if its possible to do an Auto Login
            if (_mgr.RequireUserLogin())
            {
                return;
            }

            MessageBoxResult result = MessageBoxResult.No;

            if (!_autoLogin)
            {
                result = MessageBox.Show(
                    "Credentials already saved in configuration\nChoose Yes to Auto Login or No to Reset Credentials",
                    "Auto Login", MessageBoxButton.YesNo, MessageBoxImage.Question);
            }

            if (_autoLogin || result == MessageBoxResult.Yes)
            {
                DoLogin();
            }
        }
コード例 #2
0
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     bIsConnectedComplete = false;
     mgr = new CrmConnectionManager();
     mgr.ParentControl = CrmLoginCtrl;
     mgr.UseUserLocalDirectoryForConfigStore = true;
     mgr.ClientId    = "51f81489-12ee-4a9e-aaae-a2591f45987d";
     mgr.RedirectUri = new Uri("app://58145B91-0C36-4500-8554-080854F2AC97");
     CrmLoginCtrl.SetGlobalStoreAccess(mgr);
     CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
     CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);
     CrmLoginCtrl.ConnectErrorEvent       += new EventHandler <ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);
     CrmLoginCtrl.ConnectionStatusEvent   += new EventHandler <ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);
     CrmLoginCtrl.UserCancelClicked       += new EventHandler(CrmLoginCtrl_UserCancelClicked);
     if (!mgr.RequireUserLogin())
     {
         if (MessageBox.Show("Credentials already saved in configuration\nChoose Yes to Auto Login or No to Reset Credentials", "DynamicsCrm.DevKit - Auto Login", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
         {
             CrmLoginCtrl.IsEnabled            = false;
             mgr.ServerConnectionStatusUpdate += new EventHandler <ServerConnectStatusEventArgs>(mgr_ServerConnectionStatusUpdate);
             mgr.ConnectionCheckComplete      += new EventHandler <ServerConnectStatusEventArgs>(mgr_ConnectionCheckComplete);
             mgr.ConnectToServerCheck();
             CrmLoginCtrl.ShowMessageGrid();
         }
     }
 }
コード例 #3
0
ファイル: CRMLoginForm.xaml.cs プロジェクト: MarioZG/Larawag
        /// <summary>
        /// Raised when the window loads for the first time.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            /*
             *                  This is the setup process for the login control,
             *                  The login control uses a class called CrmConnectionManager to manage the interaction with CRM, this class and also be queried as later points for information about the current connection.
             *                  In this case, the login control is referred to as CrmLoginCtrl
             */

            // Set off flag.
            bIsConnectedComplete = false;

            // Init the CRM Connection manager..
            mgr = new CrmConnectionManager();
            // Pass a reference to the current UI or container control,  this is used to synchronize UI threads In the login control
            mgr.ParentControl = CrmLoginCtrl;
            // if you are using an unmanaged client, excel for example, and need to store the config in the users local directory
            // set this option to true.
            mgr.UseUserLocalDirectoryForConfigStore = true;
            // if you are using an unmanaged client,  you need to provide the name of an exe to use to create app config key's for.
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";
            // CrmLoginCtrl is the Login control,  this sets the CrmConnection Manager into it.
            CrmLoginCtrl.SetGlobalStoreAccess(mgr);
            // There are several modes to the login control UI
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
            // this wires an event that is raised when the login button is pressed.
            CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);
            // this wires an event that is raised when an error in the connect process occurs.
            CrmLoginCtrl.ConnectErrorEvent += new EventHandler <ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);
            // this wires an event that is raised when a status event is returned.
            CrmLoginCtrl.ConnectionStatusEvent += new EventHandler <ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);
            // this wires an event that is raised when the user clicks the cancel button.
            CrmLoginCtrl.UserCancelClicked += new EventHandler(CrmLoginCtrl_UserCancelClicked);
            // Check to see if its possible to do an Auto Login
        }
コード例 #4
0
        public static void LogCrmConnectionError(Logger logger, string message, CrmConnectionManager crmConnectionManager)
        {
            string output = FormatCrmConnectionErrorOutput(crmConnectionManager);

            ExtensionLogger.LogToFile(logger, output, LogLevel.Error);

            OutputLogger.WriteToOutputWindow(output, MessageType.Error);
        }
コード例 #5
0
        /// <summary>
        /// Raised when the window loads for the first time.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Headingtext.Text = "Connecting to " + ConnectionLabel;

            /*
             *                  This is the setup process for the login control,
             *                  The login control uses a class called CrmConnectionManager to manage the interaction with CRM, this class and also be queried as later points for information about the current connection.
             *                  In this case, the login control is referred to as CrmLoginCtrl
             */

            // Set off flag.
            bIsConnectedComplete = false;

            // Init the CRM Connection manager..
            mgr = new CrmConnectionManager();
            // Pass a reference to the current UI or container control,  this is used to synchronize UI threads In the login control
            mgr.ParentControl = CrmLoginCtrl;
            // if you are using an unmanaged client, excel for example, and need to store the config in the users local directory
            // set this option to true.
            mgr.UseUserLocalDirectoryForConfigStore = true;

            // Client ID to use for oAuth.  ID: 51f81489-12ee-4a9e-aaae-a2591f45987d is a sample ID that should be replaced by a ID created for your use.
            mgr.ClientId = "51f81489-12ee-4a9e-aaae-a2591f45987d";
            // Redirect URI required for oAuth.
            mgr.RedirectUri = new Uri("app://58145B91-0C36-4500-8554-080854F2AC97");

            mgr.ProfileName = ConnectionId;

            // if you are using an unmanaged client,  you need to provide the name of an exe to use to create app config key's for.
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";
            // CrmLoginCtrl is the Login control,  this sets the CrmConnection Manager into it.
            CrmLoginCtrl.SetGlobalStoreAccess(mgr);
            // There are several modes to the login control UI
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
            // this wires an event that is raised when the login button is pressed.
            CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);
            // this wires an event that is raised when an error in the connect process occurs.
            CrmLoginCtrl.ConnectErrorEvent += new EventHandler <ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);
            // this wires an event that is raised when a status event is returned.
            CrmLoginCtrl.ConnectionStatusEvent += new EventHandler <ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);
            // this wires an event that is raised when the user clicks the cancel button.
            CrmLoginCtrl.UserCancelClicked += new EventHandler(CrmLoginCtrl_UserCancelClicked);
            // Check to see if its possible to do an Auto Login
            if (!mgr.RequireUserLogin())
            {
                // If RequireUserLogin is false, it means that there has been a successful login here before and the credentials are cached.
                CrmLoginCtrl.IsEnabled = false;
                // When running an auto login,  you need to wire and listen to the events from the connection manager.
                // Run Auto User Login process, Wire events.
                mgr.ServerConnectionStatusUpdate += new EventHandler <ServerConnectStatusEventArgs>(mgr_ServerConnectionStatusUpdate);
                mgr.ConnectionCheckComplete      += new EventHandler <ServerConnectStatusEventArgs>(mgr_ConnectionCheckComplete);
                // Start the connection process.
                mgr.ConnectToServerCheck();

                // Show the message grid.
                CrmLoginCtrl.ShowMessageGrid();
            }
        }
コード例 #6
0
 private static string FormatCrmConnectionErrorOutput(CrmConnectionManager crmConnectionManager)
 {
     return(Resource.ErrorMessage_ErrorConnecting_LastError + ": " +
            crmConnectionManager.LastError + Environment.NewLine +
            Resource.ErrorMessage_ErrorConnecting_LastException + ": " +
            crmConnectionManager.LastException.Message +
            Environment.NewLine +
            crmConnectionManager.LastException.StackTrace);
 }
コード例 #7
0
        /// <summary>
        /// Raised when the window loads for the first time.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            /*
             *                  This is the setup process for the login control,
             *                  The login control uses a class called CrmConnectionManager to manage the interaction with CRM, this class and also be queried as later points for information about the current connection.
             *                  In this case, the login control is referred to as CrmLoginCtrl
             */

            // Set off flag.
            bIsConnectedComplete = false;

            // Init the CRM Connection manager..
            mgr = new CrmConnectionManager();

            mgr.UseUserLocalDirectoryForConfigStore = true;
            mgr.ProfileName = connectionDetailId.ToString("B");
            mgr.ClientId    = AppId;
            mgr.RedirectUri = RedirectUri;

            // Pass a reference to the current UI or container control,  this is used to synchronize UI threads In the login control
            mgr.ParentControl = CrmLoginCtrl;
            // if you are using an unmanaged client, excel for example, and need to store the config in the users local directory
            // set this option to true.
            mgr.UseUserLocalDirectoryForConfigStore = true;
            // if you are using an unmanaged client,  you need to provide the name of an exe to use to create app config key's for.
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";
            // CrmLoginCtrl is the Login control,  this sets the CrmConnection Manager into it.
            CrmLoginCtrl.SetGlobalStoreAccess(mgr);
            // There are several modes to the login control UI
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
            // this wires an event that is raised when the login button is pressed.
            CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);
            // this wires an event that is raised when an error in the connect process occurs.
            CrmLoginCtrl.ConnectErrorEvent += new EventHandler <ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);
            // this wires an event that is raised when a status event is returned.
            CrmLoginCtrl.ConnectionStatusEvent += new EventHandler <ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);
            // this wires an event that is raised when the user clicks the cancel button.
            CrmLoginCtrl.UserCancelClicked += new EventHandler(CrmLoginCtrl_UserCancelClicked);
            // Check to see if its possible to do an Auto Login
            if (!mgr.RequireUserLogin() && !isUpdate)
            {
                //if (MessageBox.Show("Credentials already saved in configuration\nChoose Yes to Auto Login or No to Reset Credentials", "Auto Login", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                //{
                // If RequireUserLogin is false, it means that there has been a successful login here before and the credentials are cached.
                CrmLoginCtrl.IsEnabled = false;
                // When running an auto login,  you need to wire and listen to the events from the connection manager.
                // Run Auto User Login process, Wire events.
                mgr.ServerConnectionStatusUpdate += new EventHandler <ServerConnectStatusEventArgs>(mgr_ServerConnectionStatusUpdate);
                mgr.ConnectionCheckComplete      += new EventHandler <ServerConnectStatusEventArgs>(mgr_ConnectionCheckComplete);
                // Start the connection process.
                mgr.ConnectToServerCheck();

                // Show the message grid.
                CrmLoginCtrl.ShowMessageGrid();
                //}
            }
        }
コード例 #8
0
        private bool DoInteractiveLogin()
        {
            log.Trace($"Entered {nameof(DoInteractiveLogin)}()");

            bool flag = false;

            _connMgr = null;

            var loginDlg = new CrmLogin()
            {
                WindowStartupLocation = WindowStartupLocation.CenterScreen
            };

            loginDlg.ConnectionToCrmCompleted += LoginDlg_ConnectionToCrmCompleted;

            loginDlg.ShowDialog();

            loginDlg.ConnectionToCrmCompleted -= LoginDlg_ConnectionToCrmCompleted;

            try
            {
                if (loginDlg.CrmConnectionMgr != null &&
                    loginDlg.CrmConnectionMgr.CrmSvc != null &&
                    loginDlg.CrmConnectionMgr.CrmSvc.IsReady)
                {
                    _connMgr = loginDlg.CrmConnectionMgr;
                    _crmSvc  = _connMgr.CrmSvc;

                    flag = true;
                    log.Trace($"{nameof(_crmSvc)}.IsReady = true");
                }
                else
                {
                    if (loginDlg.CrmConnectionMgr != null)
                    {
                        if (!string.IsNullOrWhiteSpace(loginDlg.CrmConnectionMgr.LastError))
                        {
                            log.Error(loginDlg.CrmConnectionMgr.LastError);
                        }
                        if (loginDlg.CrmConnectionMgr.LastException != null)
                        {
                            log.Error(loginDlg.CrmConnectionMgr.LastException.Message, loginDlg.CrmConnectionMgr.LastException);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                log.Error($"Unexpected error: {exception.Message}", exception);
                flag = false;
            }

            log.Trace($"Exiting {nameof(DoInteractiveLogin)}()");
            return(flag);
        }
コード例 #9
0
        /// <summary>
        /// Raised when the window loads
        /// </summary>
        /// <param name="sender">The Sender</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.connectionState = false;

            // Initialize the connection Manager
            this.crmConnectionManager = new CrmConnectionManager
            {
                ParentControl = this.CrmLoginControl,
                UseUserLocalDirectoryForConfigStore = true
            };

            // Configure the Crm Control
            this.CrmLoginControl.SetGlobalStoreAccess(crmConnectionManager);
            this.CrmLoginControl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);

            // Event registration
            this.CrmLoginControl.ConnectionCheckBegining += new EventHandler(CrmLoginControl_ConnectionCheckStarted);
            this.CrmLoginControl.ConnectErrorEvent       += new EventHandler <ConnectErrorEventArgs>(CrmLoginControl_ConnectionErrorRaised);
            this.CrmLoginControl.ConnectionStatusEvent   += new EventHandler <ConnectStatusEventArgs>(CrmLoginControl_ConnectionStatusEventRaised);
            this.CrmLoginControl.UserCancelClicked       += new EventHandler(CrmLoginControl_UserCancelClicked);

            // Check if an auto login is possible
            if (!this.crmConnectionManager.RequireUserLogin())
            {
#pragma warning disable S1066 // Collapsible "if" statements should be merged
                if (MessageBox.Show("There are already Credentials saved in your configuration\nDo you want to load those?\nYes to Auto Login or No to Reset Credentials", "Auto Login possible", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
#pragma warning restore S1066 // Collapsible "if" statements should be merged
                {
                    // Credentials are cached
                    this.CrmLoginControl.IsEnabled = false;

                    this.crmConnectionManager.ServerConnectionStatusUpdate += new EventHandler <ServerConnectStatusEventArgs>(CrmConnectionManager_ServerConnectionStatusUpdateRaised);
                    this.crmConnectionManager.ConnectionCheckComplete      += new EventHandler <ServerConnectStatusEventArgs>(CrmConnectionManager_ConnectionCheckCompleted);

                    this.crmConnectionManager.ConnectToServerCheck();

                    // Show message
                    this.CrmLoginControl.ShowMessageGrid();
                }
            }
        }
コード例 #10
0
        private void btnOpenSdkLoginCtrl_Click(object sender, EventArgs e)
        {
            var ctrl = new CRMLoginForm1(connectionDetailId, !isNew);

            if (rdbUseCustom.Checked)
            {
                ctrl.AppId       = txtAzureAdAppId.Text;
                ctrl.RedirectUri = new Uri(txtReplyUrl.Text);
            }
            else
            {
                ctrl.AppId       = "51f81489-12ee-4a9e-aaae-a2591f45987d";
                ctrl.RedirectUri = new Uri("app://58145B91-0C36-4500-8554-080854F2AC97");
            }
            ctrl.ConnectionToCrmCompleted += (loginCtrl, evt) =>
            {
                ConnectionManager = ((CRMLoginForm1)loginCtrl).CrmConnectionMgr;
                SetAuthType();
                ConnectionSucceeded?.Invoke(this, null);
            };
            ctrl.ShowDialog();
        }
コード例 #11
0
        private void btnOpenSdkLoginCtrl_Click(object sender, EventArgs e)
        {
            var ctrl = new CRMLoginForm1(connectionDetailId, !isNew);

            if (rdbUseCustom.Checked)
            {
                ctrl.AppId       = txtAzureAdAppId.Text;
                ctrl.RedirectUri = new Uri(txtReplyUrl.Text);
            }
            else
            {
                ctrl.AppId       = "2ad88395-b77d-4561-9441-d0e40824f9bc";
                ctrl.RedirectUri = new Uri("app://5d3e90d6-aa8e-48a8-8f2c-58b45cc67315");
            }
            ctrl.ConnectionToCrmCompleted += (loginCtrl, evt) =>
            {
                ConnectionManager = ((CRMLoginForm1)loginCtrl).CrmConnectionMgr;
                SetAuthType();
                ConnectionSucceeded?.Invoke(this, null);
            };
            ctrl.ShowDialog();
        }
コード例 #12
0
        /// <summary>
        /// Raised when the window loads for the first time. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            /*
				This is the setup process for the login control, 
				The login control uses a class called CrmConnectionManager to manage the interaction with CRM, this class and also be queried as later points for information about the current connection. 
				In this case, the login control is referred to as CrmLoginCtrl
			 */

            // Set off flag. 
            bIsConnectedComplete = false;

            // Init the CRM Connection manager.. 
            mgr = new CrmConnectionManager();
            // Pass a reference to the current UI or container control,  this is used to synchronize UI threads In the login control
            mgr.ParentControl = CrmLoginCtrl;
            // if you are using an unmanaged client, excel for example, and need to store the config in the users local directory
            // set this option to true. 
            mgr.UseUserLocalDirectoryForConfigStore = true;
            // if you are using an unmanaged client,  you need to provide the name of an exe to use to create app config key's for. 
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";
            // CrmLoginCtrl is the Login control,  this sets the CrmConnection Manager into it. 
            CrmLoginCtrl.SetGlobalStoreAccess(mgr);
            // There are several modes to the login control UI
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);
            // this wires an event that is raised when the login button is pressed. 
            CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);
            // this wires an event that is raised when an error in the connect process occurs. 
            CrmLoginCtrl.ConnectErrorEvent += new EventHandler<ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);
            // this wires an event that is raised when a status event is returned. 
            CrmLoginCtrl.ConnectionStatusEvent += new EventHandler<ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);
            // this wires an event that is raised when the user clicks the cancel button. 
            CrmLoginCtrl.UserCancelClicked += new EventHandler(CrmLoginCtrl_UserCancelClicked);
            // Check to see if its possible to do an Auto Login 
            if (!mgr.RequireUserLogin())
            {
                if (MessageBox.Show("Credentials already saved in configuration\nChoose Yes to Auto Login or No to Reset Credentials", "Auto Login", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    // If RequireUserLogin is false, it means that there has been a successful login here before and the credentials are cached. 
                    CrmLoginCtrl.IsEnabled = false;
                    // When running an auto login,  you need to wire and listen to the events from the connection manager.
                    // Run Auto User Login process, Wire events. 
                    mgr.ServerConnectionStatusUpdate += new EventHandler<ServerConnectStatusEventArgs>(mgr_ServerConnectionStatusUpdate);
                    mgr.ConnectionCheckComplete += new EventHandler<ServerConnectStatusEventArgs>(mgr_ConnectionCheckComplete);
                    // Start the connection process. 
                    mgr.ConnectToServerCheck();

                    // Show the message grid. 
                    CrmLoginCtrl.ShowMessageGrid();
                }
            }
        }
コード例 #13
0
        /// <summary>
        /// Raised when the window loads for the first time.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // This is the setup process for the login control.
            // The login control uses a class called CrmConnectionManager to manage
            // the interaction with CRM. This class can also be queried at later points
            // for information about the current connection.
            // In this case, the login control is referred to as CrmLoginCtrl.

            // Set off flag.
            bIsConnectedComplete = false;

            // Initialize the CRM Connection manager.
            mgr = new CrmConnectionManager();

            // Pass a reference to the current UI or container control. This is used to
            // synchronize UI threads In the login control.
            mgr.ParentControl = CrmLoginCtrl;

            // If you are using an unmanaged client, say Microsoft Excel, and need to
            // store the config in the users local directory, you must set this option to true.
            mgr.UseUserLocalDirectoryForConfigStore = true;

            // If you are using an unmanaged client, you need to provide the name of
            // an executable (.exe) to use to create app config key.
            //mgr.HostApplicatioNameOveride = "MyExecName.exe";

            // This sets the CRM Connection manager for CrmLoginCtrl.
            CrmLoginCtrl.SetGlobalStoreAccess(mgr);

            // There are several modes to the login control UI.
            CrmLoginCtrl.SetControlMode(ServerLoginConfigCtrlMode.FullLoginPanel);

            // This wires an event that is raised when the login button is pressed.
            CrmLoginCtrl.ConnectionCheckBegining += new EventHandler(CrmLoginCtrl_ConnectionCheckBegining);

            // This wires an event that is raised when an error in the connect process occurs.
            CrmLoginCtrl.ConnectErrorEvent += new EventHandler <ConnectErrorEventArgs>(CrmLoginCtrl_ConnectErrorEvent);

            // This wires an event that is raised when a status event is returned.
            CrmLoginCtrl.ConnectionStatusEvent += new EventHandler <ConnectStatusEventArgs>(CrmLoginCtrl_ConnectionStatusEvent);

            // This wires an event that is raised when the user clicks the cancel button.
            CrmLoginCtrl.UserCancelClicked += new EventHandler(CrmLoginCtrl_UserCancelClicked);

            // This prompts the user to automatically sign in using the cached credentials
            // when signing for the second time or later.
            if (!mgr.RequireUserLogin())
            {
                if (MessageBox.Show("Credentials already saved in configuration.\nClick Yes to sign in using saved credentials.\nClick No to reset credentials to sign in.", "Auto Sign-In", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    // If RequireUserLogin is false, it means that there has been a successful login here before and the credentials are cached.
                    CrmLoginCtrl.IsEnabled = false;
                    // When running an auto login,  you need to wire and listen to the events from the connection manager.
                    // Run Auto User Login process, Wire events.
                    mgr.ServerConnectionStatusUpdate += new EventHandler <ServerConnectStatusEventArgs>(mgr_ServerConnectionStatusUpdate);
                    mgr.ConnectionCheckComplete      += new EventHandler <ServerConnectStatusEventArgs>(mgr_ConnectionCheckComplete);
                    // Start the connection process.
                    mgr.ConnectToServerCheck();

                    // Show the message grid.
                    CrmLoginCtrl.ShowMessageGrid();
                }
            }
        }