public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordDto dto)
        {
            ResetPasswordDtoValidator validator = new ResetPasswordDtoValidator();
            ValidationResult          result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var user = await _userManager.FindByIdAsync(dto.UserId);

                if (user == null)
                {
                    return(NotFound());
                }

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var oldSecurityStamp = user.SecurityStamp;
                        if (await _userManager.ResetPasswordAsync(user, dto.Token, dto.Password) != IdentityResult.Success)
                        {
                            throw new DbUpdateException();
                        }
                        if (await _userManager.ReplaceClaimAsync(user, new Claim(ClaimTypes.Sid, oldSecurityStamp), new Claim(ClaimTypes.Sid, user.SecurityStamp)) != IdentityResult.Success)
                        {
                            throw new DbUpdateException();
                        }
                        await transaction.CommitAsync();
                    }
                    catch (DbUpdateException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                return(NoContent());
            }
            return(BadRequest(result.Errors));
        }
Exemple #2
0
 public void Setup()
 {
     _validator = new ResetPasswordDtoValidator();
 }