public async Task <AuthResponseDTO> Login(UserCredentialsDTO dto) { ValidationResult result = _credentialsValidator.Validate(dto); if (result.Errors.Count > 0) { throw new HttpResponseException(HttpStatusCode.BadRequest, result.Errors); } User userExist = await _repository.GetByEmail(dto.Email); if (userExist == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized, new { Message = "Usuário/senha inválido(s)" }); } bool password = BCrypt.Net.BCrypt.Verify(dto.Password, userExist.Password); if (!password) { throw new HttpResponseException(HttpStatusCode.Unauthorized, new { Message = "Usuário/senha inválido(s)" }); } var token = _tokenService.GenerateToken(userExist); userExist.Password = null; return(new AuthResponseDTO { Token = token, User = _mapper.Map <UserDTO>(userExist) }); }
public static void RegisterUser(string username, string password) { var validator = new CredentialsValidator(); var validationException = validator.Validate(new ValidatedObject(username, password)); if (validationException != null) { throw new Exception($"Your {validationException.NotValidField} is not valid, " + $"Exception: {validationException.Message}"); } ApplicationRepository.AddUser(username, password); }
/// <summary> /// Verifies that the credentials are valid according to the given spec, /// if so navigation to the content page is allowed, otherwise an error is displayed to the user. /// </summary> private void OnLogin() { var results = CredentialsValidator.Validate(new LoginCredentials(Username, Password)); if (results.Any()) { var message = results.Select(v => v.Message).Aggregate((a, b) => a + "\r\n\r\n" + b); MessageBox.Show(message, AppResources.DialogErrorCaption, MessageBoxButton.OK); } else { NavigationService.NavigateTo(PageUri.ContentPageUri); } }
public ZooPark GetZoo(string username, string password) { var validator = new CredentialsValidator(); var validationException = validator.Validate(new ValidatedObject(username, password)); if (validationException != null) { throw new Exception($"Your {validationException.NotValidField} is not valid, " + $"Exception: {validationException.Message}"); } if (ApplicationRepository.CheckUserAllowedToAccess(username, password)) { return(zooLoader.GetZoo(username, password)); } throw new Exception("There is no user with such login-password match"); }