コード例 #1
0
 public async Task<ApiJsonResult> ChangePassword(ChangePasswordParams changePasswordParams)
 {
     try {
         await new AccountManager().ChangePassword(changePasswordParams);
         return new ApiJsonResult { Success = true };
     }
     catch (Exception ex) {
         return ProcessException(ex);
     }
 }
コード例 #2
0
        public async Task ChangePassword(ChangePasswordParams changePasswordParams)
        {
            Utils.CheckNullOrEmpty(new List<string> { "CurrentPassword", "NewPassword" }, changePasswordParams.CurrentPassword, changePasswordParams.NewPassword);


            using (AppDbContext context = new AppDbContext())
            {
                User user = await context.Users.FirstOrDefaultAsync(p => p.Id == changePasswordParams.UserId);
                if (user == null)
                {
                    throw new UserException(ErrorCode.INVALID.ToString());
                }

                if (!UtilsCryptography.VerifyBCryptPassword(changePasswordParams.CurrentPassword, user.Password))
                {
                    throw new UserException(ErrorCode.CURRENT_PASSWORD_INCORRECT.ToString());
                }

                user.Password = UtilsCryptography.GenerateBCryptHash(changePasswordParams.NewPassword);
                await context.SaveChangesAsync();
            }
        }