-
Notifications
You must be signed in to change notification settings - Fork 218
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
fix: #154 AddProtectedWebApiCallProtectedWebApi overwrites handler #155
fix: #154 AddProtectedWebApiCallProtectedWebApi overwrites handler #155
Conversation
… event handler AzureAD#154 Preserving existing event hander when registering OnTokenValidated event handler in AddProtectedWebApiCallsProtectedWebApi. Previous code was overwriting existing event handler meaning that logging via JwtBearerMiddlewareDiagnostics would no longer work and perhaps more seriously the OnTokenValidated event handler registered in AddProtectedWebApi (intended to "This check is required to ensure that the Web API only accepts tokens from tenants where it has been consented and provisioned.") AzureAD#154
Thanks @jg11jg. Good catch! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for raising @jg11jg
I would propose to move the call to the previous handler before storing the token
src/Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jg11jg Thanks for catching this.
options.Events ??= new JwtBearerEvents(); | ||
|
||
var onTokenValidatedHandler = options.Events.OnTokenValidated; | ||
|
||
options.Events.OnTokenValidated = async context => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmprieur Related to your suggestion we can just +=
our handler to the existing ones, right?
options.Events ??= new JwtBearerEvents(); | |
var onTokenValidatedHandler = options.Events.OnTokenValidated; | |
options.Events.OnTokenValidated = async context => | |
options.Events ??= new JwtBearerEvents(); | |
options.Events.OnTokenValidated += async context => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possibly but the convention within the rest of the library is to do it as I originally did it (in fact I have used exactly the same variable names to be consistent with the "house style". e.g:
microsoft-identity-web/src/Microsoft.Identity.Web/WebAppServiceCollectionExtensions.cs
Line 175 in d8608bb
var onTokenValidatedHandler = options.Events.OnTokenValidated; |
Possibly the += syntax requires null checks or similar? Also in the *MiddlewareDiagnostics classes += is not used. So I (personally) would stick with "house style" (or change it everywhere if += truly better).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @jg11jg, we should keep things consistent. I would leave as-is and we can update things later if we want.
Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
Code now preserves existing event handler when registering OnTokenValidated event handler in AddProtectedWebApiCallsProtectedWebApi. Previous code was overwriting existing event handler meaning that logging via JwtBearerMiddlewareDiagnostics would no longer work and perhaps more seriously the OnTokenValidated event handler registered in AddProtectedWebApi (intended to "ensure that the Web API only accepts tokens from tenants where it has been consented and provisioned.")
#154