Ejemplo n.º 1
0
        private void passwordTxt_TextChanged(object sender, EventArgs e)
        {
            var passGen = new PasswordGenerator();

            strength = passGen.DetermineStrength(passwordTxt.Text.ToString());
            passwordStrengthLbl.Text = "Password Strength: " + strength.ToString(); //dynamically update the strength of the password

            if (retypedPassTxt.Text != "")
            {
                passwordsMatchTxt.Text = passwordTxt.Text == retypedPassTxt.Text ? "Passwords match" : "Passwords do not match";
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //validate the new password
            if (newPassDupeTxt.Text == "" || newPassTxt.Text == "" || oldPassTxt.Text == "") //first, we check that the user has entered data
            {
                MessageBox.Show("All fields must contain data", "Error", MessageBoxButtons.OK);
            }
            else if (newPassTxt.Text != newPassDupeTxt.Text)
            {
                MessageBox.Show("Passwords do not match", "Error", MessageBoxButtons.OK); //secondly, we check the newly entered password is matched in the duplicated input box
            }
            else if (oldPassTxt.Text != user.Password)                                    //third, we check that the password was correct
            {
                passAttempts++;                                                           //increment the number of attempts the password has been attempted

                MessageBox.Show("Old password is incorrect.", "Error", MessageBoxButtons.OK);

                if (passAttempts == 3) //if the password attempts is 3, then we exit the system
                {
                    MessageBox.Show("Password has been entered incorrectly 3 times. System will log out");
                    this.Hide();
                    var login = new Login();
                    login.Show();
                }
            }
            else if (newPassTxt.Text == user.Password) //check that the new password does not equal the users current password
            {
                MessageBox.Show("The new password is the same as the old password.", "Error", MessageBoxButtons.OK);
            }
            else if (user.Details != null)
            {
                if (user.Details.FirstName == newPassTxt.Text || user.Details.MiddleNames == newPassTxt.Text || user.Details.LastName == newPassTxt.Text)
                {
                    MessageBox.Show("Password should not match any of your names, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
                else if (newPassTxt.Text.Contains(user.Details.FirstName) || newPassTxt.Text.Contains(user.Details.MiddleNames) || newPassTxt.Text.Contains(user.Details.LastName))
                {
                    MessageBox.Show("Password should not match or contain any of your names, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
            }
            else if (user.GuineaPigData != null)
            {
                if (user.GuineaPigData.Name == newPassTxt.Text)
                {
                    MessageBox.Show("Password should not match any of your guinea pig's name, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
                else if (newPassTxt.Text.Contains(user.GuineaPigData.Name))
                {
                    MessageBox.Show("Password should not match or contain your guinea pig's name, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
            }
            else if (gen.DetermineStrength(newPassTxt.Text) == PasswordStrength.Weak) //check the password strength
            {
                MessageBox.Show("Password is too weak to be saved.", "Invalid Password", MessageBoxButtons.OK);
            }
            else if (gen.CheckPasswordSimilarities(user.Password, newPassTxt.Text) > 10) //ensure the password is not similar to the old password
            {
                MessageBox.Show("Password is too similar to the previous password.", "Invalid Password", MessageBoxButtons.OK);
            }
            else if (passService.CheckPasswords(user.Username, newPassTxt.Text)) //check that the password has not been used before
            {
                MessageBox.Show("This password has been used before and it cannot be used again.", "Invalid Password", MessageBoxButtons.OK);
            }
            else
            {
                //if user passes all validation, we can change the password
                user.Password = newPassTxt.Text;

                if (passGenCheck.Checked) //if the password was generated, we need to save it to a file
                {
                    gen.SavePasswordToFile(newPassTxt.Text, user.Username);
                }

                service.UpdateUser(user);                           //we can then update the user
                passService.Update(user.Username, newPassTxt.Text); //update the password archive and then return to the mainUI
                MessageBox.Show("Password was changed", "Password Updated", MessageBoxButtons.OK);
                this.Hide();
                var previousForm = new MainUI(user.Id);
                previousForm.Show();
            }
        }