我有一个Azure AD应用程序,正在尝试向JWT添加自定义声明。 我正在使用Azure的声明映射功能为我的特定应用程序,并更新了Azure门户中的应用程序清单以包括可选声明。 但是,当我登录并查看解码的访问令牌时,声明在令牌中不存在。 我没有找到关于使用扩展属性作为声明的文档,但从我找到的内容来看,它应该遵循相同的模式,但实际效果并不如预期。
当用户登录时,如何将源自AD用户对象中的自定义属性的自定义声明添加到JWT?
提前感谢!
重新创建步骤
- 使用Azure AD Graph API注册目录扩展
请求:
POST https://graph.windows.net/mytenant.onmicrosoft.com/applications/<application-object-id>/extensionProperties?api-version=1.5
正文:
{
"name": "customUserRoles",
"dataType": "String",
"targetObjects": ["User"]
}
请求:
PATCH https://graph.windows.net/mytenant.onmicrosoft.com/users/user123@mytenant.onmicrosoft.com?api-version=1.5
正文:
{
"extension_<appId>_customUserRoles": "My Custom Role 1, Another Role 2"
}
- 在PowerShell中,我安装了Azure AD模块:
Install-Module -Name AzureADPreview
- 创建一个Azure AD策略
New-AzureADPolicy -Definition @('{"ClaimsMappingPolicy":{"Version": 1, "IncludeBasicClaimSet": "true", "
ClaimsSchema": [ { "Source": "user", "ID": "extension_<appId>_customUserRoles", "JwtClaimType": "customUserRoles" } ] } }') -DisplayName "customUserRoles" -Type "ClaimsMappingPolicy"
- 将策略添加到服务主体
Add-AzureADServicePrincipalPolicy -Id <service-principla-id> -RefObjectId <azure-ad-policy-id>
- 在Azure门户中,导航到Azure AD -> 应用程序注册 -> 我的应用程序 -> Manifest
- 更新以下属性
{
...
"acceptMappedClaims: true,
"optionalClaims": {
"idToken": [
{
"name": "extension_<appId>_customUserRoles",
"source": "user",
"essential": false,
}
],
"accessToken": [
{
"name": "extension_<appId>_customUserRoles",
"source": "user",
"essential": false,
}
],
"samlToken": []
}
}
- 保存文件
- 导航到
https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/authorize?client_id=<appId>&response_type=token&resource=https://mytenant.sharepoint.com
并使用 Azure AD 用户账户user123@mytenant.onmicrosoft.com
登录 - 在 URL 中,复制
access_token
参数的值 - 导航到
https://jwt.ms
并将访问令牌粘贴到文本区域中 - 在解码的令牌部分,自定义声明 customUserRoles 不存在
我期望能在解码的令牌中看到一个名为 customUserRoles
或 extn.customUserRoles
的新声明。
我错过了哪些步骤?在整个过程中我没有收到任何错误提示,但它似乎没有按照文档所述的那样工作。
参考资料
我已经阅读了微软关于以下主题的文档:
可选声明: https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-optional-claims
声明映射: https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-claims-mapping
我还阅读了各种与此相关的论坛帖子和博客文章:
https://devonblog.com/cloud/azure-ad-adding-employeeid-claims-in-azure-ad-jwt-token/