public ActionResult Edit(User userFromForm) { var userToUpdate = _userRepository.Find(userFromForm.Id); userToUpdate.Email = userFromForm.Email; userToUpdate.Name = userFromForm.Name; userToUpdate.Comment = userFromForm.Comment; if (ModelState.IsValid) { _userRepository.InsertOrUpdate(userToUpdate); _userRepository.Save(); return RedirectToAction("Index"); } else { return View(userToUpdate); } }
private User SetNewPassword(User user, string password) { var passwordSalt = BCryptHelper.GenerateSalt(12); var passwordHash = BCryptHelper.HashPassword(password, passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; user.DateLastPasswordChange = DateTime.Now; return user; }
public RegisterUserViewModel RegisterUser(User user) { var viewModel = new RegisterUserViewModel(); viewModel.User = user; // check if email exists if (GetByEmail(user.Email) != null) { viewModel.HasError = true; viewModel.Message = "Email address is already in use"; return viewModel; } viewModel.GeneratedPassword = GeneratePassword(8); viewModel.User = SetNewPassword(viewModel.User, viewModel.GeneratedPassword); viewModel.User.PasswordNeedsUpdating = true; viewModel.User.DateCreated = DateTime.Now; viewModel.User.DateLastActivity = DateTime.Now; viewModel.User.DateLastLogin = DateTime.Now; viewModel.User.DateLastPasswordChange = DateTime.Now; context.Users.Add(viewModel.User); context.SaveChanges(); return viewModel; }
public void InsertOrUpdate(User user) { if (user.Id == default(int)) { // New entity context.Users.Add(user); } else { // Existing entity context.Entry(user).State = EntityState.Modified; } }
public ActionResult Register(User user) { var userDto = _userRepository.RegisterUser(user); if (ModelState.IsValid) { if (!userDto.HasError) { // SEND EMAIL TO USER var siteroot = string.Format("{0}{1}", Request.DomainApplicationPath().TrimEnd('/'), Url.Action("ChangePassword", "Account")); _userMailer.Welcome(userDto.User.Name, userDto.User.Email, userDto.GeneratedPassword, siteroot).Send(); TempData["SuccessMessage"] = "The user was successfully registered."; return RedirectToAction("Edit", new {id = userDto.User.Id}); } ViewData.ModelState.AddModelError("_FORM", userDto.Message); } userDto.User = user; //MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, userDto.User.ValidationResults()); return View(userDto.User); }