public ActionResult PasswordChange(ChangePasswordModel model) { User authentacatedUser = _userRepository.GetUserByPhone(User.Identity.Name); if (authentacatedUser != null) { string error; if (_passwordValidator.IsPasswordValid(model.OldPassword, authentacatedUser)) { if (model.IsValid(out error)) { authentacatedUser.Password = _passwordEncryptor.EncryptPassword(model.Password); _userRepository.SaveChanges(); TempData["message"] = Translation.Translation.UserDataSavedMessage; } else { ModelState.AddModelError("", error); } } else { error = Translation.Translation.ProfileOldPasswordIsNotCorrectMessage; ModelState.AddModelError("", error); } return(View(model)); } else { throw new Exception(Translation.Translation.AccessIsDeniedMessage); } }
public async Task <IActionResult> ChangePassword(ChangePasswordModel model) { DateTime dtBeforeCalling = DateTime.Now; AdminRequestLog log = new AdminRequestLog(); log.UserIP = Utilities.GetUserIPAddress(); log.ServerIP = Utilities.GetInternalServerIP(); log.UserAgent = Utilities.GetUserAgent(); log.PageName = "ChangePassword"; log.PageURL = "/ChangePassword"; // log.ApiURL = Utilities.GetCurrentURL(httpContext); log.MethodName = "ChangePassword"; log.UserID = User.GetUserId(); log.UserName = User.GetUserName(); var output = new Output <LoginOutput>(); try { output = model.IsValid <ChangePasswordModel, LoginOutput>(); if (output.ErrorCode != Output <LoginOutput> .ErrorCodes.Success) { log.ServiceResponseTimeInSeconds = DateTime.Now.Subtract(dtBeforeCalling).TotalSeconds; log.ErrorDescription = output.ErrorDescription; log.ErrorCode = (int)output.ErrorCode; LogService.AddAdminRequestLogs(log); return(Ok(output)); } var user = userManager.FindByEmailAsync(model.Email).Result; if (user == null) { output.ErrorCode = Output <LoginOutput> .ErrorCodes.NotFound; output.ErrorDescription = ResourcesHepler.GetMessage("UserNotFound", model.Language); log.ServiceResponseTimeInSeconds = DateTime.Now.Subtract(dtBeforeCalling).TotalSeconds; log.ErrorDescription = "User not found"; log.ErrorCode = (int)output.ErrorCode; LogService.AddAdminRequestLogs(log); return(Ok(output)); } user.PasswordConfirmed = true; string token = userManager.GeneratePasswordResetTokenAsync(user).Result; var res = await userManager.ResetPasswordAsync(user, token, model.Password); if (res.Succeeded) { output.ErrorCode = Output <LoginOutput> .ErrorCodes.Success; log.ServiceResponseTimeInSeconds = DateTime.Now.Subtract(dtBeforeCalling).TotalSeconds; log.ErrorDescription = "password changed successfully"; log.ErrorCode = (int)output.ErrorCode; LogService.AddAdminRequestLogs(log); return(Ok(output)); } output.ErrorCode = Output <LoginOutput> .ErrorCodes.Failed; output.ErrorDescription = "changePasswordFailed"; log.ServiceResponseTimeInSeconds = DateTime.Now.Subtract(dtBeforeCalling).TotalSeconds; log.ErrorDescription = "failed to change password"; log.ErrorCode = (int)output.ErrorCode; LogService.AddAdminRequestLogs(log); return(Ok(output)); } catch (Exception ex) { output.ErrorCode = Output <LoginOutput> .ErrorCodes.ServerException; output.ErrorDescription = ResourcesHepler.GetMessage("ServerException", model.Language); log.ServiceResponseTimeInSeconds = DateTime.Now.Subtract(dtBeforeCalling).TotalSeconds; log.ErrorDescription = ex.ToString(); log.ErrorCode = (int)output.ErrorCode; LogService.AddAdminRequestLogs(log); return(Ok(output)); } }