public async Task <IActionResult> Login([FromBody] UserRequest userRequest) { if (userRequest == null) { _logger.LogError("Login: userRequest object sent from client is null."); return(BadRequest("userRequest object is null")); } if (!ModelState.IsValid) { _logger.LogError("Login: Invalid userRequest object sent from client."); return(BadRequest("Invalid userRequest object")); } var user = await _userManager.FindByNameAsync(userRequest.UserName); if (user == null) { _logger.LogError("Login: User does not exist."); return(BadRequest("User does not exist")); } AppUsageLog appUsageLog; if (!await _userManager.CheckPasswordAsync(user, userRequest.Password)) { _logger.LogError("Login: Incorrect password."); appUsageLog = new AppUsageLog { Message = $"Failed login attempt for user {userRequest.UserName}", Tag = "IdentityController", UserId = 0, ReportedOn = DateTime.Now }; _coadaptService.AppUsageLog.CreateAppUsageLog(appUsageLog); await _coadaptService.SaveAsync(); return(BadRequest("Incorrect password")); } var roles = await _userManager.GetRolesAsync(user); var jwt = CreateJWT(user, roles); var userId = await _coadaptService.GetCoadaptUserIdByRole(user.Id, roles); appUsageLog = new AppUsageLog { Message = "User login", Tag = "IdentityController", UserId = userId, ReportedOn = DateTime.Now }; _coadaptService.AppUsageLog.CreateAppUsageLog(appUsageLog); await _coadaptService.UserAccessToken.UpsertRefreshTokenAsync(user.Id, jwt.RefreshToken); await _coadaptService.SaveAsync(); return(Ok(new LoginResponse { Id = user.Id, UserName = user.UserName, Roles = roles, Token = jwt.Token, Expires = jwt.Expires, RefreshToken = jwt.RefreshToken })); }