public async Task <ActionResult> LoginAsync([FromBody] Authentication.Request authenticateRequest) { _logger.Info("Login request from User: {0}", authenticateRequest.UserName); var authenticationResponse = await _userService.Authenticate(authenticateRequest); if (authenticationResponse is null) { _logger.Info("Username or password is incorrect or user is inactive."); return(BadRequest("Username or password is incorrect or user is inactive.")); } _logger.Info("User {0} successfully logged in", authenticateRequest.UserName); return(Ok(authenticationResponse)); }
/// <summary> /// Authenticates the user and creates the auth. response. /// Returns null if authentication failed or user is not active. /// </summary> public async Task <Authentication.Response> Authenticate(Authentication.Request request) { // creates credentials obj and prepares the username Credential userCredential = CreateCredential(request.UserName.TrimAndLower(), request.Password); _logger.Debug("Created Credential for user: {0}", request.UserName); var user = await _userDispatcher.CheckUserCredentialAsync(userCredential); // authentication failed if CheckUserCredentialAsync // didnt find the user or the pws dont match OR if the // user is not active. if (user is null || !user.Active.GetValueOrDefault()) { return(null); } var jwt = GenerateJwt(user.Id.Value); return(new Authentication.Response(user, jwt, int.Parse(_configuration["Jwt:ExpirationMinutes"]))); }