public async Task <TDto> CreateAsync <TDto>(UserAdd dto) where TDto : class, IUserViewable { var user = mapper.Map <ApplicationUser>(dto); var password = passwordUtils.CreateHash("Parola123!"); user.SetHashAndSalt(password.Hash, password.Salt); await CreateAsync(user); return(mapper.Map <TDto>(user)); }
public async Task PasswordResetAsync(ValidationDto dto) { var validationResult = await tokenService.ValidateTokenAsync(dto.Token); if (!validationResult.IsValid) { throw new SecurityTokenException(); } if (dto.NewPassword != dto.ConfirmPassword) { throw new PasswordMismatchException("Passwords are different!"); } var user = await context.Users.SingleOrDefaultAsync(auth => auth.Id == validationResult.User.Id); if (user is null) { throw new EntityNotFoundException <ApplicationUser>(); } if (!passwordUtils.VerifyHash(dto.OldPassword, user.PasswordHash, user.PasswordSalt)) { throw new EntityNotFoundException <ApplicationUser>(); } var securedPassword = passwordUtils.CreateHash(dto.NewPassword); user.SetHashAndSalt(securedPassword.Hash, securedPassword.Salt); await UpdateAsync(user); }
public async Task <RegistrationView> RegisterAsync(RegistrationDto dto) { var existingUser = await context.Users.SingleOrDefaultAsync(auth => auth.Username == dto.Username); if (existingUser != null) { throw new AuthenticationException(); } var user = mapper.Map <ApplicationUser>(dto); var securedPassword = passwordUtils.CreateHash(dto.Password); user.SetHashAndSalt(securedPassword.Hash, securedPassword.Salt); await userService.CreateAsync(user); return(mapper.Map <RegistrationView>(user)); }