AzureADB2C.UI - access to OpenIdConnectEvents (OnTokenValidated)

六月ゝ 毕业季﹏ 提交于 2020-06-29 04:27:44

问题


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

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.

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.

Many thanks


回答1:


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;
        },   
    };
});


来源:https://stackoverflow.com/questions/61488529/azureadb2c-ui-access-to-openidconnectevents-ontokenvalidated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!