private void signUpBtn_Click(object sender, EventArgs e) { try { ctx = new UsefDataEF.UsersDBEntities(); ctx.LoginInformations.Load(); if (!checkData()) { return; } revertColor(); byte[] salt = new HashGetter().getSalt(); UsefDataEF.LoginInformation newUser = new UsefDataEF.LoginInformation(); newUser.Username = UsernameTB.Text; newUser.UserPassword = new HashGetter().getHash(PasswordTB.Text, salt); newUser.UserSalt = salt; newUser.UserEmail = emailTB.Text; ctx.LoginInformations.Add(newUser); ctx.SaveChanges(); MessageBox.Show("Successfully signed up!", "Success"); Close(); } catch (Exception) { MessageBox.Show("Error signing up", "Error"); } }
private void forgotPasswordBtn_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(emailTB.Text)) { emailTB.BackColor = Color.Red; return; } UsefDataEF.UsersDBEntities ctx = new UsefDataEF.UsersDBEntities(); var query = from c in ctx.LoginInformations where c.UserEmail == emailTB.Text select c; if (query.Any()) { string username = query.First().Username; string password = CreatePassword(12); byte[] salt = new HashGetter().getSalt(); byte[] hashedPassword = new HashGetter().getHash(password, salt); query.First().UserSalt = salt; query.First().UserPassword = hashedPassword; ctx.SaveChanges(); emailPassword(query.First().UserEmail, query.First().Username, password); } else { MessageBox.Show("No such E-mail address", "Error"); } }
private void logInBtn_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(LoginTB.Text) || String.IsNullOrWhiteSpace(PasswordTB.Text)) { MessageBox.Show("Username and password are mandatory", "Invalid Data"); return; } UsefDataEF.UsersDBEntities ctx = new UsefDataEF.UsersDBEntities(); var query = from c in ctx.LoginInformations where c.Username == LoginTB.Text select c; if (query.Count() == 0) { MessageBox.Show("No such user", "No user"); return; } byte[] salt = query.First().UserSalt; byte[] hashedPass = new HashGetter().getHash(PasswordTB.Text, salt); if (query.Where(c => c.UserPassword == hashedPass).Count() > 0) { success = true; Close(); } else { MessageBox.Show("Invalid username or password", "Error"); } }
private void changePasswordBtn_Click(object sender, EventArgs e) { if (!checkData()) { return; } revertColor(); var query = from c in ctx.LoginInformations select c; byte[] salt = query.Where(c => c.Username == UsernameTB.Text).First().UserSalt; byte[] hashedPassword = new HashGetter().getHash(oldPasswordTB.Text, salt); var user = (from c in query where c.Username == UsernameTB.Text select c into z where z.UserPassword == hashedPassword select z); if (user.Any()) { byte[] newSalt = new HashGetter().getSalt(); byte[] newHashedPassword = new HashGetter().getHash(PasswordTB.Text, newSalt); user.First().UserSalt = newSalt; user.First().UserPassword = newHashedPassword; ctx.SaveChanges(); MessageBox.Show("Password changed", "Success"); Close(); } }