I am using Azure B2C in a ASP.NET Core 3 application, which is working perfectly. I use the following code in Startup:
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
I would like to handle the standard TokenValidated OpenIdConnect event, with other words I need a configuration where my event handler is set.
Examining the source code I see the class AzureAdB2COpenIDConnectEventHandlers.cs
and also its usage in AzureADB2COpenIdConnectOptionsConfiguration
but unfortunately both class declared to internal
Question
All I need is to have my TokenValidated handler in effect, retaining all working out of the box OpenIdConnect based AD B2C functionality, which is working currently.
Pseudo code, something like this:
options.Events = new OpenIdConnectEvents()
{
// ...
OnTokenValidated = MyTokenValidatedHandler
};
How can I accomplish this in a simple way?
I found my answer, by searching for ["Events.OnTokenValidated" AzureAdB2C] in github, and assembled the following for my case:
// My existing code in Startup:
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
// My added code to handle the OnTokenValidated event
services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.OpenIdScheme, options =>
{
var onTokenValidated = options.Events.OnTokenValidated;
options.Events.OnTokenValidated = context =>
{
onTokenValidated?.Invoke(context);
// My custom handler goes below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With