public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (db.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "is taken."); } } if (ModelState.IsValid == false) { return(View("Index")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); db.Users.Add(newUser); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", newUser.UserId); return(RedirectToAction("Success")); }
public IActionResult CreateRegistration(Registration register) { // Check initial ModelState if (ModelState.IsValid) { // If a User exists with provided email if (db.Registrations.Any(r => r.Email == register.Email)) { // Manually add a ModelState error to the Email field, with provided // error message ModelState.AddModelError("Email", "Email already in use!"); return(View("Index", register));// You may consider returning to the View at this point } PasswordHasher <Registration> Hasher = new PasswordHasher <Registration>(); register.Password = Hasher.HashPassword(register, register.Password); db.Add(register); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", register.UserId); return(RedirectToAction("Success")); } else { return(View("Index"));// other code } }