AzureADB2C.UI-访问OpenIdConnectEvents(OnTokenValidated) [英] AzureADB2C.UI - access to OpenIdConnectEvents (OnTokenValidated)

查看:144
本文介绍了AzureADB2C.UI-访问OpenIdConnectEvents(OnTokenValidated)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用库 AzureADB2C.UI 启用Azure ADB2C身份验证.

I use the library AzureADB2C.UI to enable Azure ADB2C authentication.

但是现在我想在身份验证后添加一个自定义声明,我想在OpenIdConnectEvents.OnTokenValidated期间执行此操作.但是这些事件并未公开.

But now I would like to add a custom claim after authentication and I wanted to do this during OpenIdConnectEvents.OnTokenValidated. But those events are not exposed.

在这种情况下,有什么建议最合适的方法是添加自定义声明?并且最好继续使用该软件包,以避免过多的自定义代码.我尝试了遵循SO的方法,但这没有用

Any suggestion what the most appropriate way is to add a custom claim in this situation? And preferable keep on using the package to avoid too much custom code. I tried the following on SO but this didn't work out.

非常感谢

推荐答案

您可以参考下面的代码示例,将声明添加到用户原则中:

You can refer to below code sample to add claims into user's principle :

services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
            .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));

services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.OpenIdScheme, options =>
{

    options.Events = new OpenIdConnectEvents
    {

        OnTokenValidated =  ctx =>
        {
            //query the user's groups using api 

            // add claims
            var claims = new List<Claim>
            {
                new Claim("groups", xxxx-xx-xx)
            };
            var appIdentity = new ClaimsIdentity(claims);

            ctx.Principal.AddIdentity(appIdentity);

            return Task.CompletedTask;
        },   
    };
});

这篇关于AzureADB2C.UI-访问OpenIdConnectEvents(OnTokenValidated)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆