Skip to content

Instantly share code, notes, and snippets.

@bbarry
Created August 19, 2020 19:02
Show Gist options
  • Save bbarry/f313ef409a7512af60b47fb522752c06 to your computer and use it in GitHub Desktop.
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
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