public IActionResult Register(User user) { if (ModelState.IsValid) { if (dbContext.Users.Any(u => u.Email == user.Email)) { ModelState.AddModelError("Email", "This Email already exist"); return(View("Index")); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); dbContext.Add(user); dbContext.SaveChanges(); HttpContext.Session.SetString("email", user.Email); return(RedirectToAction("Success")); } else { return(View("Index")); } }
public IActionResult Register(UserObject user) { // Check initial ModelState 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, with provided error message ModelState.AddModelError("Email", "Email already in use!"); // You may consider returning to the View at this point return(View("Index", user)); } else { // Initializing a PasswordHasher object, providing our User class as its PasswordHasher <UserObject> Hasher = new PasswordHasher <UserObject>(); user.Password = Hasher.HashPassword(user, user.Password); //Save your user object to the database dbContext.Add(user); dbContext.SaveChanges(); HttpContext.Session.SetInt32("userid", user.UserId); return(Redirect("/success")); //This doesn't exist yet } } // other code else { return(View("Index", user)); } }
public IActionResult AddMessage(Message newMessage) { int?userId = HttpContext.Session.GetInt32("UserId"); if (userId == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { newMessage.UserId = (int)userId; dbContext.Add(newMessage); dbContext.SaveChanges(); return(RedirectToAction("Dashboard")); } return(View("Dashboard")); }
public IActionResult CreateWedding(Wedding newWedding) { if (ModelState.IsValid) { int?userId = HttpContext.Session.GetInt32("UserId"); newWedding.UserId = (int)userId; dbContext.Add(newWedding); dbContext.SaveChanges(); return(RedirectToAction("ViewWedding", new { weddingId = newWedding.WeddingId })); } else { return(View("AddWedding")); } }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { var userInDb = dbContext.Users.FirstOrDefault(u => u.Email == newUser.Email); if (userInDb != null) { ModelState.AddModelError("Email", "This email already taken"); return(View("Index")); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.Add(newUser); dbContext.SaveChanges(); var userToLogIn = dbContext.Users.FirstOrDefault(u => u.Email == newUser.Email); HttpContext.Session.SetInt32("UserId", userToLogIn.UserId); return(RedirectToAction("Success")); } else { return(View("Index")); } }
public IActionResult AddCategory(Category newCategory) { int?userId = HttpContext.Session.GetInt32("UserId"); if (userId == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { newCategory.CreatorId = (int)userId; dbContext.Add(newCategory); dbContext.SaveChanges(); return(RedirectToAction("CreateCategory")); } else { List <Category> categories = dbContext.Categories .Where(category => category.CreatorId == userId).ToList(); ViewBag.Categories = categories; return(View("CreateCategory")); } }
public IActionResult CreateUser(User user) { if (ModelState.IsValid) { if (db.Users.Any(u => u.Email == user.Email)) { ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); db.Add(user); db.SaveChanges(); return(RedirectToAction("LoginPage")); } return(View("Index")); }
public IActionResult Register(User newUser) { if (dbContext.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "That Email already exists!"); } if (ModelState.IsValid) { PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); HttpContext.Session.SetInt32("ID", newUser.Id); dbContext.Add(newUser); dbContext.SaveChanges(); return(RedirectToAction("Success")); } return(View("Index")); }
public IActionResult CreateWedding(Wedding newWedding) { int?userId = HttpContext.Session.GetInt32("UserId"); if (userId == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var oneUser = dbContext.Users.FirstOrDefault(w => w.UserId == userId); newWedding.CreatorName = oneUser.FirstName; newWedding.UserId = (int)userId; dbContext.Add(newWedding); dbContext.SaveChanges(); return(RedirectToAction("Dashboard")); } return(View("AddWedding")); }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (db.Users.Any(user => user.Email == newUser.Email)) { ModelState.AddModelError("Email", "This email is already registered."); return(View("Index")); } PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); db.Add(newUser); db.SaveChanges(); HttpContext.Session.SetInt32("userId", newUser.UserId); return(RedirectToAction("Success")); } return(View("Index")); }