Beispiel #1
0
        private void createUserBtn_Click(object sender, EventArgs e)
        {
            //validate the password, output relevant messages to inform the user of the situation
            if (usernameTxt.Text == "" || passwordTxt.Text == "" || retypedPassTxt.Text == "") //first check we have data in the input fields
            {
                MessageBox.Show("Username and password fields are required to register.", "error", MessageBoxButtons.OK);
            }
            else if (passwordTxt.Text != retypedPassTxt.Text) //check that the new password and the retyped version match
            {
                MessageBox.Show("Passwords do not match.");
            }
            else if (!userService.CheckUsernameIsUnique(usernameTxt.Text)) //check the username is unique
            {
                MessageBox.Show("Username already exists.", "Error", MessageBoxButtons.OK);
            }
            else if (ValidateUsername(usernameTxt.Text)) //check the username does not contain special characters
            {
                MessageBox.Show("Username cannot contain special characters", "Error", MessageBoxButtons.OK);
            }
            else if (helper.CheckAgainstPhrases(passwordTxt.Text) == PasswordStrength.Weak)
            {
                MessageBox.Show("Password matches common phrases or the example password.", "Error", MessageBoxButtons.OK);
            }
            else if (strength == PasswordStrength.Weak) //check the password strength is not weak
            {
                MessageBox.Show("Password is too weak", "error", MessageBoxButtons.OK);
            }
            else
            {
                //then we can create the user, and add them to db. a new id is generated at the service layer.
                var user = new User
                {
                    Username     = usernameTxt.Text.ToString(),
                    Password     = passwordTxt.Text.ToString(),
                    PasswordUses = 0,
                };

                userService.AddUser(user);
                var passService = new PasswordService();

                var newPassword = new PasswordArchive //create a new password archive and store the users first password
                {
                    ID        = user.Id,
                    Username  = user.Username,
                    Passwords = new System.Collections.Generic.List <string>(),
                };

                newPassword.Passwords.Add(user.Password);

                passService.Put(newPassword);

                if (generatePassCheck.Checked) //if the password was generated, save the password to a file
                {
                    var passGen = new PasswordGenerator();
                    passGen.SavePasswordToFile(passwordTxt.Text, usernameTxt.Text);
                    MessageBox.Show("Generated Password was saved to project folders", "Password Saved", MessageBoxButtons.OK);
                }

                MessageBox.Show(generatePassCheck.Checked ? "User created! Password was saved to a file.": "User Created!", "User Created", MessageBoxButtons.OK);
                this.Hide();
                var mainUi = new MainUI(user.Id);
                mainUi.Show();
            }
        }