Exemplo n.º 1
0
        /// <summary>
        /// Tests the current connection until the user enters a correct password.
        /// </summary>
        /// <param name="wbConnection">A <see cref="MySqlWorkbenchConnection"/> object representing the connection to a MySQL server instance selected by users.</param>
        /// <param name="tryConnectionBeforeAskingForPassword">Flag indicating whether a connection test is made with the connection as is before asking for a password</param>
        /// <returns>A <see cref="PasswordDialogFlags"/> containing data about the operation.</returns>
        public static PasswordDialogFlags TestConnectionAndRetryOnWrongPassword(this MySqlWorkbenchConnection wbConnection, bool tryConnectionBeforeAskingForPassword = true)
        {
            PasswordDialogFlags passwordFlags = new PasswordDialogFlags(wbConnection)
            {
                // Assume a wrong password at first so if the connection is not tested without a password we ensure to ask for one.
                ConnectionResult = TestConnectionResult.WrongPassword
            };

            // First connection attempt with the connection exactly as loaded (maybe without a password).
            if (tryConnectionBeforeAskingForPassword)
            {
                passwordFlags.ConnectionResult = wbConnection.TestConnectionAndReturnResult(false);
                passwordFlags.Cancelled        = passwordFlags.ConnectionResult == TestConnectionResult.PasswordExpired;

                // If on the first attempt a connection could not be made and not because of a bad password, exit.
                if (!passwordFlags.ConnectionSuccess && !passwordFlags.WrongPassword)
                {
                    return(passwordFlags);
                }
            }

            // If the connection does not have a stored password or the stored password failed then ask for one and retry.
            while (!passwordFlags.ConnectionSuccess && passwordFlags.WrongPassword)
            {
                passwordFlags = PasswordDialog.ShowConnectionPasswordDialog(wbConnection, true);
                if (passwordFlags.Cancelled)
                {
                    break;
                }

                wbConnection.Password = passwordFlags.NewPassword;
            }

            return(passwordFlags);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Event delegate method fired when the <see cref="PasswordDialog"/> form is closing.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void PasswordDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Cancel)
            {
                _passwordFlags.Cancelled = true;
                return;
            }

            if (PasswordExpiredDialog)
            {
                // Check if the new password and its confirmation match, otherwise notify the user and exit.
                if (NewPasswordTextBox.Text != ConfirmTextBox.Text)
                {
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Properties.Resources.PasswordResetErrorTitleText, Properties.Resources.PasswordsMismatchErrorText));
                    e.Cancel = true;
                    return;
                }

                // Reset the password and if the reset is successful assign the new password to the local connection.
                _wbConnection.Password = PasswordTextBox.Text;
                try
                {
                    _wbConnection.ResetPassword(ConfirmTextBox.Text);
                }
                catch (Exception ex)
                {
                    MySqlSourceTrace.WriteAppErrorToLog(ex);
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Properties.Resources.PasswordResetErrorTitleText, Properties.Resources.PasswordResetErrorDetailText));
                    _passwordFlags.Cancelled = true;
                    return;
                }

                _wbConnection.Password = ConfirmTextBox.Text;
            }
            else
            {
                _wbConnection.Password = PasswordTextBox.Text;
            }

            _passwordFlags.NewPassword = _wbConnection.Password;
            bool connectionSuccessful = false;

            if (_testConnection)
            {
                // Test the connection and if not successful revert the password to the one before the dialog was shown to the user.
                TestConnectionResult connectionResult = _wbConnection.TestConnectionAndReturnResult(false);
                _passwordFlags.ConnectionResult = connectionResult;
                switch (connectionResult)
                {
                case TestConnectionResult.ConnectionSuccess:
                case TestConnectionResult.PasswordReset:
                    connectionSuccessful = true;

                    // If the pasword was reset within the connection test, then set it again in the new password flag.
                    if (connectionResult == TestConnectionResult.PasswordReset)
                    {
                        _passwordFlags.NewPassword = _wbConnection.Password;
                    }

                    break;

                case TestConnectionResult.PasswordExpired:
                    // This status is set if the password was expired, and the dialog shown to the user to reset the password was cancelled, so exit.
                    return;
                }
            }

            // If the connection was successful and the user chose to store the password, save it in the password vault.
            if (!StorePasswordSecurely || !connectionSuccessful || string.IsNullOrEmpty(_wbConnection.Password))
            {
                return;
            }

            string storedPassword = MySqlWorkbenchPasswordVault.FindPassword(_wbConnection.HostIdentifier, _wbConnection.UserName);

            if (storedPassword == null || storedPassword != _wbConnection.Password)
            {
                MySqlWorkbenchPasswordVault.StorePassword(_wbConnection.HostIdentifier, _wbConnection.UserName, _wbConnection.Password);
            }
        }