Created
August 19, 2020 19:02
-
-
Save bbarry/f313ef409a7512af60b47fb522752c06 to your computer and use it in GitHub Desktop.
validate a token according to the asp.net core application's configured options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JwtValidationService : ITokenService | |
{ | |
private readonly JwtBearerOptions _options; | |
public JwtTokenService(IOptions<JwtBearerOptions> options) | |
{ | |
_options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | |
} | |
public bool ValidateToken(string token) | |
{ | |
var parameters = _options.TokenValidationParameters; | |
foreach (var tokenValidator in _options.SecurityTokenValidators) | |
{ | |
try | |
{ | |
tokenValidator.ValidateToken(token, parameters, out _); | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment