使用AD B2C时如何添加OnTokenValidated事件处理程序? [英] How to add OnTokenValidated event handler when using AD B2C?
问题描述
我正在ASP.NET Core 3应用程序中使用Azure B2C,该应用程序运行良好. 我在启动中使用以下代码:
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));
我想处理标准的TokenValidated OpenIdConnect事件,换句话说,我需要一个设置了事件处理程序的配置.
I would like to handle the standard TokenValidated OpenIdConnect event, with other words I need a configuration where my event handler is set.
检查源代码,我看到了类AzureAdB2COpenIDConnectEventHandlers.cs
及其在AzureADB2COpenIdConnectOptionsConfiguration
中的用法,但不幸的是,这两个类都已声明为internal
Examining the source code I see the class AzureAdB2COpenIDConnectEventHandlers.cs
and also its usage in AzureADB2COpenIdConnectOptionsConfiguration
but unfortunately both class declared to internal
问题
我需要做的是使我的TokenValidated处理程序有效,保留所有现成的基于OpenIdConnect的AD B2C功能.
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.
伪代码,如下所示:
options.Events = new OpenIdConnectEvents()
{
// ...
OnTokenValidated = MyTokenValidatedHandler
};
如何以一种简单的方式完成此任务?
How can I accomplish this in a simple way?
推荐答案
我通过在github中搜索["Events.OnTokenValidated" AzureAdB2C]找到了答案,并为我的案例组装了以下内容:
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:
这篇关于使用AD B2C时如何添加OnTokenValidated事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!