【发布时间】:2020-03-15 08:35:45
【问题描述】:
我有一个 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"]
}
- 为特定 AD 用户向扩展写入值
请求:
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 -> 应用注册 -> 我的应用 -> 清单
- 更新以下属性
{
...
"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://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-optional-claims
索赔映射:https://docs.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/
【问题讨论】:
-
我认为您将无法在访问令牌中为您未创建的 API 获取自定义声明。据我了解,针对您应用的 API 的访问令牌以及您的应用收到的 Id 令牌可能包含它们。
-
如果特定声明具有值,那么它将出现在令牌中。空值从令牌中完全过时。通过调用 graph.microsoft.com beta 端点检查用户配置文件中的值。喜欢:graph.microsoft.com/beta/users/… 并查看价值。
标签: azure sharepoint jwt azure-active-directory access-token