简体   繁体   English

添加Azure Ad Oauth2 JWT令牌声明

[英]Adding Azure Ad Oauth2 JWT Token Claims

I was just wondering if there is a way to add or specify custom claims to the Azure Ad OAuth2 JWT token via Azure Portal? 我只是想知道是否有办法通过Azure门户添加或指定Azure Ad OAuth2 JWT令牌的自定义声明? Or is this only possible code side? 或者这只是可能的代码方面?

As far as I know, the Azure AD doesn't support to issue the custom claim at present. 据我所知,Azure AD目前不支持发布自定义声明。

As a workaround, we can use the Azure AD Graph to add the directory schema extensions . 作为解决方法,我们可以使用Azure AD Graph添加目录架构扩展 After that, we can use the Azure AD Graph to get the data extension and add the custom claim when the security token is verified like code below: 之后,我们可以使用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);
            }
    });

In addition if you have any idea or feedback about Azure, you can submit them from here . 此外,如果您对Azure有任何想法或反馈,可以从此处提交。

I believe that you could get an example on how to set additional claims (Role claims for instance) by reading the How to run the sample as a single-tenant app part of the Authorization in a web app using Azure AD application roles & role claims Azure-AD sample. 我相信您可以通过阅读如何 使用Azure AD应用程序角色和角色声明在Web应用程序中作为授权 的单租户应用程序部分运行示例来获取有关如何设置其他声明(例如,角色声明) 的示例 Azure-AD示例。 This requires editing the Azure-AD application manifest to add application roles. 这需要编辑Azure-AD应用程序清单以添加应用程序角色。 Then assign different roles to different users in the directory 然后为目录中的不同用户分配不同的角色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM