Exemplo n.º 1
0
        public IActionResult Create(UsersCreateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            if (Models.User.IsUserNameInUse(Database.Connection, viewModel.UserName))
            {
                ModelState.AddModelError(nameof(UsersCreateViewModel.UserName), "Username is already in use.");

                return(View(viewModel));
            }

            string hashedPassword = authenticationBusinessLogic.HashPassword(viewModel.Password);
            User   user           = new User(viewModel.UserName, hashedPassword);

            user.Save(Database.Connection);

            TempData["Message"] = "User saved successfully.";

            return(RedirectToAction(nameof(Edit), new { id = user.Id }));
        }
Exemplo n.º 2
0
        public IActionResult Edit(AccountEditViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            if (authenticationBusinessLogic.VerifyPassword(ApplicationUser.Password, viewModel.OldPassword))
            {
                ModelState.AddModelError(nameof(AccountEditViewModel.OldPassword), "Old password is incorrect.");

                return(View(viewModel));
            }

            ApplicationUser.Password = authenticationBusinessLogic.HashPassword(viewModel.NewPassword);
            ApplicationUser.Save(Database.Connection);

            TempData["Message"] = "Account saved successfully.";

            return(RedirectToAction(nameof(Edit)));
        }