public ActionResult Add(UserAdminEdit userModel) { ViewData["Roles"] = _dbContext.Roles; if (!ModelState.IsValid) return View(userModel); // Validate Password if (userModel.Password == "") { ModelState.AddModelError("Password", "You must enter a password."); return View(userModel); } // Move VM into the User Model Object var user = new User { Username = userModel.Username, Password = Pbkdf2Crypto.ComputeHash(userModel.Password), Email = userModel.Email, RoleId = userModel.RoleId, RegisterDate = userModel.RegisterDate, LastSigninDate = userModel.LastSigninDate }; // Save to DB _dbContext.Users.Add(user); _dbContext.SaveChanges(); return RedirectToAction("Index", "Users"); }
public ActionResult Edit(UserAdminEdit userModel) { ViewData["Roles"] = _dbContext.Roles; if (!ModelState.IsValid) return View(userModel); // Get User var user = _dbContext.Users.Find(userModel.Id); // Hash new password, if there isn't one, use old pass userModel.Password = userModel.Password == "" ? user.Password : Pbkdf2Crypto.ComputeHash(userModel.Password); // Move VM into the User Model Object user.Username = userModel.Username; user.Password = userModel.Password; user.Email = userModel.Email; user.RoleId = userModel.RoleId; user.RegisterDate = userModel.RegisterDate; user.LastSigninDate = userModel.LastSigninDate; // Save to DB _dbContext.Entry(user).State = EntityState.Modified; _dbContext.SaveChanges(); return RedirectToAction("Index", "Users"); }