c# – 添加Azure Ad Oauth2 JWT令牌声明

我只是想知道是否有办法通过Azure门户添加或指定Azure Ad OAuth2 JWT令牌的自定义声明?

或者这只是可能的代码方面? 最佳答案 据我所知,Azure AD目前不支持发布自定义声明.

作为解决方法,我们可以使用Azure AD Graph添加directory schema extensions.之后,我们可以使用Azure AD Graph获取数据扩展,并在验证安全令牌时添加自定义声明,如下所示:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = authority,
        PostLogoutRedirectUri = postLogoutRedirectUri,
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = context => 
            {
                context.HandleResponse();
                context.Response.Redirect("/Error?message=" + context.Exception.Message);
                return Task.FromResult(0);
            }
            ,
            SecurityTokenValidated = context =>
            {
                //you can use the Azure AD Graph to read the custom data extension here and add it to the claims 
                context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim("AddByMe", "test"));
                return Task.FromResult(0);
            }
    });

此外,如果您对Azure有任何想法或反馈,可以从here提交.

点赞