-
Notifications
You must be signed in to change notification settings - Fork 428
IdentityModel Validation model change #2711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
New token validation modelThe existing token validation model on As part of an effort to modernize aspects of the library that have become bloated over the years, we are introducing an alternative token validation model that breaks away from some of the previous behaviors and intends to offer a slimmer and more performant API. Key points on the new model
Embracing the Result pattern to remove exceptions on a hot pathIn order to remove the exceptions being thrown, we are introducing a The
The The following example attempts to illustrate this: Creating a ValidationResult// ValidationResult<TResult> can contain TResult if valid, or ValidationError if not.
ValidationResult<string> issuerValidationResult = "some-issuer"; // valid, creates a successful result with "some-issuer" as the Result
issuerValidationResult.IsValid // true
issuerValidationResult.Result // "some-issuer"
issuerValidationResult.Error // null
ValidationResult<string> issuerValidationResult2 = new IssuerValidationError(...) // valid, creates a failed result with the new instance of IssuerValidationError as the Error, which inherits from ValidationError and adds extra information such as the invalid issuer
issuerValidationResult2.IsValid // false
issuerValidationResult2.Result // null
issuerValidationResult2.Error // the IssuerValidationError instance Validating a token using the new validation modelDuring the initial preview, the new validation methods are not exposed publicly in string token = "some JWT token";
ValidationParameters validationParameters = new ValidationParameters()
{
ValidAudiences = ["http://Default.Audience.com"],
ValidIssuers = ["http://Default.Issuer.com"],
IssuerSigningKeys = [KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key]
};
CallContext callContext = new CallContext();
JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler();
ValidationResult<ValidatedToken> validationResult = await ((IResultBasedValidation)jsonWebTokenHandler).ValidateTokenAsync(token, validationParameters, callContext, default);
if (validationResult.IsValid)
// do something with the ValidatedToken returned.
ValidatedToken validatedToken = validationResult.Result;
else
// inspect the error, log it to telemetry, etc
ValidationError validationError = validationResult.Error; Examples of this can be found in dev/test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests_e2e.cs. Benchmarks
|
Any chance of getting an async version of |
Uh oh!
There was an error while loading. Please reload this page.
IdentityModel is responsible for validating SecurityTokens. Validating a SecurityToken requires validating multiple parts and reporting the results. Common parts to validate are the issuer, audience, expiration. Default validation is included for important parts of the SecurityToken. The current model provides extensibility using delegates for validation.
Issues we want to address:
Proposal
Each validation step will return a specialized ValidationResult type that contain details that will provide upper layers to the examine errors with contain exception details, log details with a stacktrace that can be thrown or logged.
Related PRs: #2709, #2688, #2679, #2672, #2671, #2669, #2655
The text was updated successfully, but these errors were encountered: