public async Task <IActionResult> UpdatePassword(int Id, UserUpdatePasswordDTO userUpdatePassword) { try { var user = await this._userManager.FindByIdAsync(Id.ToString()); if (user == null) { return(this.StatusCode(StatusCodes.Status404NotFound, "Usuário não encontrado")); } await this._userManager.RemovePasswordAsync(user); var result = await this._userManager.AddPasswordAsync(user, userUpdatePassword.Password); if (result.Succeeded) { return(Ok()); } return(BadRequest(result.Errors)); } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Banco de dados falhou")); } }
public async Task <ActionResult <UULResponse> > ChangePassword(UserUpdatePasswordDTO userPwdsDTO) { if (!userPwdsDTO.isValid(out var msg)) { return(Error.ProfileValidationFailed.CreateErrorResponse(_logger, "ChangePassword", new Exception(msg))); } UULResponse response; try { var userInfoDTO = await AuthenticateUserOrThrow(userPwdsDTO.toLoginInfoDTO()); var user = await UserDao.GetUserByDetailsOrThrow(_context, userInfoDTO.Login, userInfoDTO.ApartmentCode); var salt = SecHelper.CreateSalt(); user.Salt = salt; user.Hash = SecHelper.SaltAndHashPwd(userPwdsDTO.NewPwd, salt); _context.Users.Update(user); await _context.SaveChangesAsync(); var tokenString = SecHelper.GenerateJSONWebToken(userInfoDTO.Login, userInfoDTO.ApartmentCode, _config); var habitants = await _context.Habitants.Where(h => h.User.ID == user.ID).Select(h => new HabitantDTO(h)).ToListAsync(); response = new UULResponse() { Success = true, Message = tokenString, Data = new UserInfoDTO(user, habitants) }; } catch (UserProfileNotFoundException e) { response = Error.ProfileNotFound.CreateErrorResponse(_logger, "ChangePassword", e); } catch (AuthException e) { response = Error.AuthFailed.CreateErrorResponse(_logger, "ChangePassword", e); } catch (Exception e) { response = Error.ProfileChangePwdFailed.CreateErrorResponse(_logger, "ChangePassword", e); } return(response); }
public async Task UpdatePasswordAsync(UserUpdatePasswordDTO model) { User user = await _userManager.FindByIdAsync(model.Id); if (user == null) { throw new ArgumentNullException(nameof(user)); } var result = await _userManager.ChangePasswordAsync(user, model.Password, model.NewPassword); if (result.Succeeded == false) { throw new Exception("Some thing go wrong during password update. Please try again"); } }
public async Task <IActionResult> UpdatePassword([FromForm] UserUpdatePasswordDTO model) { try { if (ModelState.IsValid) { await _identityService.UpdatePasswordAsync(model); return(RedirectToAction(nameof(Index))); } return(View(model)); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(View(model)); } }
public async Task <IActionResult> UpdatePassword([FromBody] UserUpdatePasswordDTO request, [FromRoute] string id) { try { User user = _userService.GetById(id); if (user == null) { return(NotFound()); } user.Password = request.Password; _userService.Update(user); return(Ok(user)); } catch { return(StatusCode((int)HttpStatusCode.InternalServerError)); } }