private void TestGenerateSalt() { string firstSalt = HashProfile.GenerateSalt(); string secondSalt = HashProfile.GenerateSalt(); Assert.NotEqual(firstSalt, secondSalt); }
public async Task <IActionResult> Register([Bind("Login, Password, ConfirmPassword, Email")] RegisterViewModel registerViewModel) { try { if (ModelState.IsValid) { registerViewModel.Password = HashProfile.GetSaltedHashData(registerViewModel.Password, registerViewModel.PasswordSalt); DbUser userModel = _mapper.Map <DbUser>(registerViewModel); DbContext.Add(userModel); await DbContext.SaveChangesAsync(); if (SendConfirmationEmail(userModel)) { return(RedirectToAction("Login", "Auth")); } else { ModelState.AddModelError("", "Unable to send confirmation e-mail. "); DbContext.Users.Remove(userModel); await DbContext.SaveChangesAsync(); return(View(registerViewModel)); } } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(registerViewModel)); }
public async Task <IActionResult> EditPost(RegisterViewModel registerViewModel) { if (ModelState.IsValid) { var userToUpdate = await DbContext.Users.FirstOrDefaultAsync(s => s.UserId == int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier))); if (await TryUpdateModelAsync <DbUser>(userToUpdate, "", s => s.Login, s => s.Email, s => s.Password)) { try { userToUpdate.Password = HashProfile.GetSaltedHashData(userToUpdate.Password, userToUpdate.PasswordSalt); await DbContext.SaveChangesAsync(); CookieProfile.UpdateUserCookie(HttpContext, userToUpdate); return(RedirectToAction("Index", "Tasks")); } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } } } return(View(registerViewModel)); }
public async Task <IActionResult> EditPasswordPost(PasswordViewModel passwordViewModel, string token, int id) { if (ModelState.IsValid) { var userToUpdate = await DbContext.Users.FirstOrDefaultAsync(s => s.UserId == id); if (await TryUpdateModelAsync <DbUser>(userToUpdate, "", s => s.Password)) { try { userToUpdate.Password = HashProfile.GetSaltedHashData(userToUpdate.Password, userToUpdate.PasswordSalt); await DbContext.SaveChangesAsync(); return(RedirectToAction("Login", "Auth")); } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } } } return(View(passwordViewModel)); }
private void TestValidatePasswordsTrue() { string password = "******"; string salt = HashProfile.GenerateSalt(); string hashedPassword = HashProfile.GetSaltedHashData(password, salt); Assert.True(HashProfile.ValidatePasswords(password, hashedPassword, salt)); }
private void TestValidatePasswordsDifferentPasswords() { string password = "******"; string secondPassword = "******"; string salt = HashProfile.GenerateSalt(); string hashedPassword = HashProfile.GetSaltedHashData(password, salt); Assert.False(HashProfile.ValidatePasswords(secondPassword, hashedPassword, salt)); }
private void TestValidatePasswordsDifferentSalts() { string password = "******"; string correctSalt = HashProfile.GenerateSalt(); string incorrectSalt = HashProfile.GenerateSalt(); string hashedPassword = HashProfile.GetSaltedHashData(password, correctSalt); Assert.False(HashProfile.ValidatePasswords(password, hashedPassword, incorrectSalt)); }
private void TestGetSaltedHashData() { string password = "******"; string salt = HashProfile.GenerateSalt(); string firstSaltedHashPassword = HashProfile.GetSaltedHashData(password, salt); string secondSaltedHashPassword = HashProfile.GetSaltedHashData(password, salt); Assert.Equal(firstSaltedHashPassword, secondSaltedHashPassword); }
private void TestGetSaltedHashDataDifferentPasswords() { string firstPassword = "******"; string secondPassword = "******"; string salt = HashProfile.GenerateSalt(); string firstSaltedHashPassword = HashProfile.GetSaltedHashData(firstPassword, salt); string secondSaltedHashPassword = HashProfile.GetSaltedHashData(secondPassword, salt); Assert.NotEqual(firstSaltedHashPassword, secondSaltedHashPassword); }
public async Task <IActionResult> Create([Bind("Login, Password, Email")] UserViewModel userViewModel) { try { if (ModelState.IsValid) { userViewModel.Password = HashProfile.GetSaltedHashData(userViewModel.Password, userViewModel.PasswordSalt); DbUser userModel = _mapper.Map <DbUser>(userViewModel); DbContext.Add(userModel); await DbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(userViewModel)); }
public async Task <IActionResult> Login(LoginViewModel loginViewModel) { if (!ModelState.IsValid) { return(View("Login", loginViewModel)); } DbUser user = await DbContext.Users.Where(s => s.Login == loginViewModel.Login).SingleOrDefaultAsync(); if (user == null || !HashProfile.ValidatePasswords(loginViewModel.Password, user.Password, user.PasswordSalt)) { ModelState.AddModelError("", "Wrong login or password"); return(View("Login", loginViewModel)); } if (!user.EmailConfirmed) { ModelState.AddModelError("", "Verify your email address"); return(View("Login", loginViewModel)); } CookieProfile.SignUserCookie(HttpContext, user); return(RedirectToAction("Index", "Tasks")); }
private string CreateUserToken(DbUser user) { string token = HashProfile.GetSaltedHashData(user.Email, user.PasswordSalt); return(token); }