public IActionResult Create(User user) { if (ModelState.IsValid) { // If a User exists with provided email if (dbContext.Users.Any(u => u.Email == user.Email)) { // Manually add a ModelState error to the Email field ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } // hash password PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); // create user dbContext.Add(user); dbContext.SaveChanges(); // sign user into session var NewUser = dbContext.Users.FirstOrDefault(u => u.Email == user.Email); int UserId = NewUser.UserId; HttpContext.Session.SetInt32("UserId", UserId); // go to success return(RedirectToAction("Dashboard")); } // display errors else { return(View("Index")); } }
public IActionResult Register(User used) { System.Console.WriteLine("entered reg+++++++++++++++++++++++++++++++"); //validating submission against models if (ModelState.IsValid) { System.Console.WriteLine("passed model validation++++++++++++++++++++++++++"); //verifying the email address is not already in use if (dbContext.Users.Any(u => u.email == used.email)) { System.Console.WriteLine("failed email validation+++++++++++++++++++++++++++++++++++++"); //ading email / password error to display to user ModelState.AddModelError("email", "Email already in use!"); return(View("Index")); } System.Console.WriteLine("everything passed hashing PW+++++++++++++++++++++++"); //hasshing users password before saving to the database and saving to user instance PasswordHasher <User> Hasher = new PasswordHasher <User>(); used.password = Hasher.HashPassword(used, used.password); //Save your user object to the database System.Console.WriteLine("Password Hashed adding to DB++++++++++++++"); dbContext.Add(used); System.Console.WriteLine("Password Hashed saving to DB++++++++++++++"); dbContext.SaveChanges(); System.Console.WriteLine("created new user sending to success page++++++++++++++"); HttpContext.Session.SetString("Login", "True"); User user = dbContext.Users.FirstOrDefault(u => u.email == used.email); HttpContext.Session.SetInt32("id", user.UserId); return(RedirectToAction("Success")); } System.Console.WriteLine("modelstate is valid failed++++++++++++++++++++++++++++++++++++++++"); return(View("Index")); }