public async Task <UserCreationResult> TryCreateUser(string userName, string password) { userName = userName?.Trim(); if (string.IsNullOrWhiteSpace(userName)) { return(new UserCreationResult("The user name is required")); } if (await _userRepo.IsUserNameTakenAsync(userName)) { return(new UserCreationResult($"The user name {userName} is already taken")); } string pwError = _passwordValidator.ValidatePassword(password); if (!string.IsNullOrWhiteSpace(pwError)) { return(new UserCreationResult(pwError)); } byte[] passwordHash = _hashEncoder.EncodeToHash(password); var user = new User { UserNameDisplay = userName, PasswordHash = passwordHash }; await _userRepo.SaveAsync(user); return(new UserCreationResult(user)); }
public async Task <LogInResponse> LogInAsync(LogInRequest request) { if (string.IsNullOrEmpty(request.Login)) { throw new ArgumentException(); } if (string.IsNullOrEmpty(request.Password)) { throw new ArgumentException(); } var user = await _usersRepository.GetUserByLoginAsync(request.Login); if (_passwordValidator.ValidatePassword(request.Password, user.Password, user.Salt)) { return(await _tokenService.LogInAsync(user.Role)); } throw new ArgumentException("Invalid username or password"); }