private async Task <IdentityUser> ValidateUser(Auth.LoginRequest credentials) { var identityUser = await _userManager.FindByNameAsync(credentials.Username); if (identityUser == null) { return(null); } var result = _userManager.PasswordHasher.VerifyHashedPassword(identityUser, identityUser.PasswordHash, credentials.Password); return(result == PasswordVerificationResult.Failed ? null : identityUser); }
public async Task <IActionResult> Login([FromBody] Auth.LoginRequest request) { IdentityUser identityUser; _logger.LogInformation($"Login request from {request.Username}"); if (!ModelState.IsValid || request == null || (identityUser = await ValidateUser(request)) == null) { _logger.LogError($"Login failed for {request.Username}"); return(new BadRequestObjectResult(new { Message = "Login failed" })); } var newToken = _tokenProvider.GenerateToken(identityUser); _logger.LogInformation($"Login successful for {request.Username}"); return(Ok(new { Token = newToken, Message = "Success" })); }