添加Azure Ad Oauth2 JWT令牌声明

我只是想知道是否有办法通过Azure门户添加或指定Azure Ad OAuth2 JWT令牌的自定义声明? 或者这只是可能的代码方面?

据我所知,Azure AD目前不支持发布自定义声明。

作为解决方法,我们可以使用Azure AD Graph添加目录架构扩展 。 之后,我们可以使用Azure AD Graph获取数据扩展,并在validation安全令牌时添加自定义声明,如下所示:

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有任何想法或反馈,可以从此处提交。

我相信您可以通过阅读如何 使用Azure AD应用程序角色和角色声明在Web应用程序中作为授权 的单租户应用程序部分运行示例来获取有关如何设置其他声明(例如,角色声明) 的示例 Azure-AD示例。 这需要编辑Azure-AD应用程序清单以添加应用程序角色。 然后为目录中的不同用户分配不同的角色