【问题标题】:Azure AD Custom Claims in JWTJWT 中的 Azure AD 自定义声明
【发布时间】:2020-03-15 08:35:45
【问题描述】:

我有一个 Azure AD 应用程序,我正在尝试向 JWT 添加自定义声明。我正在为我的特定应用程序使用 Azure 中的声明映射功能,并更新了 Azure 门户中的应用程序清单以包含可选声明。但是,当我登录并查看解码的访问令牌时,令牌中不存在声明。我没有找到太多与使用扩展属性作为声明相关的文档,但据我发现它应该遵循相同的模式,但它没有按预期工作。

当用户登录时,如何将源自 AD 中用户对象中的自定义属性的自定义声明添加到 JWT?

提前致谢!

重新创建的步骤

  1. 使用 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"]
}
  1. 为特定 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"
}
  1. 在 PowerShell 中,我安装了 Azure AD 模块:Install-Module -Name AzureADPreview
  2. 创建 Azure AD 策略
New-AzureADPolicy -Definition @('{"ClaimsMappingPolicy":{"Version": 1, "IncludeBasicClaimSet": "true", "
ClaimsSchema": [ { "Source": "user", "ID": "extension_<appId>_customUserRoles", "JwtClaimType": "customUserRoles" } ] } }') -DisplayName "customUserRoles" -Type "ClaimsMappingPolicy"
  1. 将策略添加到服务主体
Add-AzureADServicePrincipalPolicy -Id <service-principla-id> -RefObjectId <azure-ad-policy-id>
  1. 在 Azure 门户中,导航到 Azure AD -> 应用注册 -> 我的应用 -> 清单
  2. 更新以下属性
{
   ...
   "acceptMappedClaims: true,
   "optionalClaims": {
      "idToken": [
         {
            "name": "extension_<appId>_customUserRoles",
            "source": "user",
            "essential": false,
         }
      ],
      "accessToken": [
         {
            "name": "extension_<appId>_customUserRoles",
            "source": "user",
            "essential": false,
         }
      ],
      "samlToken": []
   }
}
  1. 保存文件
  2. 导航到https://login.microsoftonline.com/mytenant.onmicrosoft.com/oauth2/authorize?client_id=&lt;appId&gt;&amp;response_type=token&amp;resource=https://mytenant.sharepoint.com 并使用Azure AD 用户帐户user123@mytenant.onmicrosoft.com 登录
  3. 在 URL 中,复制 access_token 参数的值
  4. 导航到https://jwt.ms 并将访问令牌粘贴到文本区域中
  5. 在解码的令牌部分,自定义声明 customUserRoles 存在

我的期望是我应该在解码的令牌中看到一个名为 customUserRolesextn.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/

http://www.redbaronofazure.com/?p=7566

https://social.msdn.microsoft.com/Forums/en-US/3e5114b6-24d6-4c60-b72b-b4c90baeecac/access-token-missing-optional-claims-that-are-schema-extensions-implicit-grant-flow

https://social.msdn.microsoft.com/Forums/en-US/dbeeed63-8d3f-4c27-b416-431f9fe6c729/providing-directory-extension-optional-claims-and-returning-value-within-token?forum=WindowsAzureAD

【问题讨论】:

  • 我认为您将无法在访问令牌中为您未创建的 API 获取自定义声明。据我了解,针对您应用的 API 的访问令牌以及您的应用收到的 Id 令牌可能包含它们。
  • 如果特定声明具有值,那么它将出现在令牌中。空值从令牌中完全过时。通过调用 graph.microsoft.com beta 端点检查用户配置文件中的值。喜欢:graph.microsoft.com/beta/users/… 并查看价值。

标签: azure sharepoint jwt azure-active-directory access-token


【解决方案1】:

基于this official 文档:

访问令牌始终使用资源清单生成, 不是客户。所以在请求中 ...范围=https://graph.microsoft.com/user.read...资源是 图形。因此,访问令牌是使用 Graph 清单创建的,而不是 客户的清单。更改应用程序的清单将 永远不要让 Graph 的标记看起来不同。为了验证 如果您的 accessToken 更改生效,请为您的 应用程序,而不是其他应用程序。

并且根据您的要求,如果您想对访问令牌进行一些更改,该资源是在线共享点,它是由 MSFT 创建和管理的多租户应用程序,这是不可能的。

对于this doc,我也为你做了一些研究。同样,您应该控制服务端应用程序,以便实现这一点。

这是我的策略角色分配命令:

$nsp = New-AzureADPolicy -Definition @('{"ClaimsMappingPolicy":{"Version":1,"IncludeBasicClaimSet":"true", "ClaimsSchema": [{"Source":"user","ID":"mailnickname","JwtClaimType":"testclaim"}]}}') -DisplayName "StanCustomCliamDemo_surname" -Type "ClaimsMappingPolicy"

Add-AzureADServicePrincipalPolicy  -RefObjectId $nsp.Id -Id '<obj id of service side app>'

令牌结果:

此外,请注意extension_&lt;appId&gt;_customUserRoles 不是有效的用户源ID。所有有效的用户源ID,请参考here

希望对您有所帮助。

【讨论】:

  • 服务主体应用注册和自定义扩展字段对我不起作用...
  • @Stanley,我也有类似的要求。我想向 azure 广告访问令牌添加一些自定义的用户定义声明。我的要求完全符合stackoverflow.com/questions/60569243/…
  • @stanleyGong 你是如何传递值“stan”的,这是在你尝试获取令牌时在请求期间动态传递的吗?
猜你喜欢
  • 1970-01-01
  • 2022-10-07
  • 2021-08-06
  • 2023-04-10
  • 1970-01-01
  • 2017-09-09
  • 2021-06-30
  • 2018-03-28
  • 2023-03-24
相关资源
最近更新 更多