private void registerButton_Click(object sender, EventArgs e) { string userName = userNameTextBox.Text; string password = passwordTextBox.Text; string emailAddress = emailTextBox.Text; if (!isValidUserName(userName)) { MessageBox.Show("The username must have at least 3 characters and can contain only lowercase(a-z) and uppercase(A-Z) letters, digits(0-9) and underscores(_)!", "User registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (password.Length < minimumPasswordLength) { MessageBox.Show("Your password should be at least 10 characters long! Please try again.", "User registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!isValidPassword(password)) { MessageBox.Show("Invalid password! Your password must contain:\n1.Lowercase and uppercase letters (a-zA-z) \n2.Digits (0-9) \n3.Special characters (@#$%<>?)", "User registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!isValidEmail(emailAddress)) { MessageBox.Show("Invalid email address!", "User registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (userExists(getUser(sqlStatementCheckUserExistence, userName))) { MessageBox.Show("The selected username already exists! Please try again", "User registration", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } ConfirmationSender emailSender = new ConfirmationSender(); string emailSubject = "New user creation"; string emailBody = "A user creation request was made for an account that will associated to this email address.\nPlease enter the following code to finish user creation process and confirm your email: {0} \nIf you have not made such a request please ignore this email and delete it."; string onSuccessMessage = "An email containing the confirmation code for the new user creation was sent to the specified email address"; string parentWindowName = "Register"; string generatedConfirmationCode = emailSender.generateConfirmationCode(); emailSender.sendConfirmationEmail(emailAddress, emailSubject, emailBody, generatedConfirmationCode, onSuccessMessage, parentWindowName); String userInputConfirmationCode = Interaction.InputBox("Enter the code received on your email to finish the user creation process:", "Confirmation Code", "Enter code", 200, 200); if (emailSender.confirmationCodesMatch(generatedConfirmationCode, userInputConfirmationCode)) { PasswordSecurityManager securityManager = new PasswordSecurityManager(); byte[] salt = securityManager.getSalt(16); string hashCode = securityManager.createPasswordHash(password, salt); MySqlCommand userCreationCommand = SQLCommandBuilder.getNewUserCreationCommand(sqlStatementCreateNewUser, userName, salt, hashCode, emailAddress); int executionResult = DBConnectionManager.insertData(userCreationCommand); if (executionResult == -1) { MessageBox.Show("Could not create the requested user!", "Register", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show("Your user was succesfully created!", "Register", MessageBoxButtons.OK, MessageBoxIcon.Information); clearInputFields(textBoxes); registerButton.Enabled = false; } else { MessageBox.Show("Invalid confirmation code! Please try again.", "Register", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void resetPasswordButton_Click(object sender, EventArgs e) { if (!DBConnectionManager.hasConnection()) { //MessageBox.Show(this, "No database connection! Unable to reset your password.", "Password reset manager"); MessageBox.Show(this, "No database connection! Unable to reset your password.", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Cere confirmarea utilizatorului pentru resetarea parolei si inregistreaza rezultatul //DialogResult userOption = MessageBox.Show(this, "Are you sure that you want to reset your password?", "Password reset manager", MessageBoxButtons.YesNo); DialogResult userOption = MessageBox.Show(this, "Are you sure that you want to reset your password?", "Password reset manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question); //Daca se selecteaza optiunea "No" se iese din metoda si se opreste procesul de resetare if (userOption == DialogResult.No) { //Console.WriteLine("User selected NO option"); return; } //Adunare date necesare resetarii parolei String userName = userNameTextBox.Text; String newPassword = newPasswordTextBox.Text; String confirmationPassword = confirmPasswordTextBox.Text; //Verifica daca noua parola si parola de confirmare sunt identice if (!newPassword.Equals(confirmationPassword)) { //MessageBox.Show("The input passwords don't match! Please try again!", "Password reset manager"); MessageBox.Show("The input passwords don't match! Please try again!", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //Verifica daca parola respecta regulile de complexitate if (newPassword.Length < minimumPasswordLength) { //MessageBox.Show("Your password should be at least 10 characters long! Please try again.", "Password reset manager"); MessageBox.Show("Your password should be at least 10 characters long! Please try again.", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!isValidPassword(newPassword)) { //MessageBox.Show("Invalid password! Your password must contain:\n1.Lowercase and uppercase letters (a-zA-z) \n2.Digits (0-9) \n3.Special characters (@#$%<>?)", "Password reset manager"); MessageBox.Show("Invalid password! Your password must contain:\n1.Lowercase and uppercase letters (a-zA-z) \n2.Digits (0-9) \n3.Special characters (@#$%<>?)", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } MySqlCommand retrieveResetPasswordDataCommand = new MySqlCommand(sqlStatementGetDataForPasswordReset); retrieveResetPasswordDataCommand.Parameters.AddWithValue("@paramUserName", userName); DataTable resetPasswordDataTable = DBConnectionManager.getData(retrieveResetPasswordDataCommand); //Se verifica daca exista utilizatorul if (userExists(resetPasswordDataTable)) { Object emailData = resetPasswordDataTable.Rows[0].ItemArray[2]; String userEmail = emailData != DBNull.Value ? emailData.ToString() : ""; //Se verifica daca utilizatorul are setata o adresa de email if ("".Equals(userEmail)) { //MessageBox.Show("Unable to retrieve the email address for the selected user!", "Password reset manager"); MessageBox.Show("Unable to retrieve the email address for the selected user!", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Se creeaza obiectele necesare pt procesul de resetare doar daca sunt indeplinite toate conditiile anterioare //Obiectul pt resetarea efectiva a parolei PasswordResetManager passwordResetManager = new PasswordResetManager(); //Obiectul pt trimiterea codului de confirmare ConfirmationSender confirmationSender = new ConfirmationSender(); //Date necesare pt trimiterea emailului string emailSubject = "Password reset"; string emailBody = "A password reset was requested for the account associated to this email address.\nPlease enter the following code to finish the password reset process: {0} \nIf you have not requested the password reset please ignore this email and delete it."; string onSuccessMessage = "An email containing the reset password procedure has been sent to your email address"; string parentWindowName = "Password reset manager"; string generatedConfirmationCode = confirmationSender.generateConfirmationCode(); confirmationSender.sendConfirmationEmail(userEmail, emailSubject, emailBody, generatedConfirmationCode, onSuccessMessage, parentWindowName); String userInputConfirmationCode = Interaction.InputBox("Enter the code received on your email to finish the reset process:", "Confirmation Code", "Enter code", 200, 200); if (confirmationSender.confirmationCodesMatch(generatedConfirmationCode, userInputConfirmationCode)) { int userID = Convert.ToInt32(resetPasswordDataTable.Rows[0].ItemArray[0]); int executionResult = passwordResetManager.resetPassword(newPassword, userID);//Daca din diverse motive nu se pot insera in baza de date noile informatii(salt si hashcode) metoda returneaza -1 if (executionResult == -1) { //MessageBox.Show("Could not reset your password!", "Password reset manager"); MessageBox.Show("Could not reset your password!", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //MessageBox.Show("Your password has been succesfully reset!", "Password reset manager"); MessageBox.Show("Your password has been succesfully reset!", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { //Se afiseaza mesajul doar daca codul introdus nu se potriveste cu cel generat nu si in situatia in care //utilizatorul selecteaza optiunea Cancel sau inchide fereastra if (!"".Equals(userInputConfirmationCode)) { //MessageBox.Show("Invalid confirmation code! Please try again.", "Password reset manager"); MessageBox.Show("Invalid confirmation code! Please try again.", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { //MessageBox.Show("Invalid username!", "Password reset manager"); MessageBox.Show("Invalid username!", "Password reset manager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } TextBox[] textBoxes = new TextBox[] { newPasswordTextBox, confirmPasswordTextBox }; clearInputFields(textBoxes); resetPasswordButton.Enabled = false; }