AAD 组声称某些用户在 JWT 令牌中丢失

AAD groups claim missing in JWT token for some users

我在 AAD 上遇到了一些奇怪的行为。用户成功登录后,我们的 API 通话中的一些用户获得了未授权。原来 JWT 中的声明丢失了。一些用户正在获得 "groups" 声明(他所属的所有 groupId 的数组),而一些用户正在获得 "hasgroups" 声明(如果用户有组,则为布尔值,没有 ID)。由于我们的 API 应用正在检查此 "groups" 授权声明,因此没有此 "groups" 声明的用户将收到 403。

尽管如此,在应用程序注册的清单中,我将“groupMembershipClaims”从“null”设置为 "All" 或 "SecurityGroup",这应该可以同时实现。同时将 "oauth2AllowImplicitFlow" 设置为 true,因为我们正在使用使用 OAuth2 的 Angular 应用程序。接下来我比较了几乎所有的用户设置,除了一些额外的组之外,用户是相同的。受影响的用户没有很多组,有些甚至最多只有 5 个组。

我是不是忽略了什么或者是什么导致了这种索赔差异?我该如何解决这个问题,以便所有用户都能获得 "groups" 声明?

从 MSFT 内部获得此反馈:

In the implicit flow, oauth will return the Jwt directly from the intial /authorize call through a query string param. The http spec limits the length of a query string / url, so if AAD detects that the resulting URI would be exceeding this length, they replace the groups with the hasGroups claim.

还有这个

This is by design when using implicit grant flow, regardless the "groupMembershipClaims" setting in the manifest. It's to avoid to go over the URL length limit of the browser as the token is returned as a URI fragment. So, more or less after 4 user's groups membership, you'll get "hasgroups:true" in the token. What you can do is to make a separate call to the Graph API to query for the user's group membership.

因此需要额外往返 Graph API 以获得用户组。希望这对其他人也有帮助。

这现在记录在 https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-token-and-claims 的 Azure AD 令牌参考中。

对于 OAuth2 隐式授权流程,它使用 hasGroups 令牌,该令牌的文档说明:

Used in place of the groups claim for JWTs in implicit grant flows if the full groups claim would extend the URI fragment beyond the URL length limits (currently 6 or more groups).

对于其他流程:

if the number of groups the user is in goes over a limit (150 for SAML, 200 for JWT) then an overage claim will be added the claim sources pointing at the Graph endpoint containing the list of groups for the user.

您可以使用图表 API 通过 https://graph.windows.net/{tenantID}/users/{userID}/getMemberObjects 获取用户组。

或者,在 https://graph.windows.net/myorganization/isMemberOf?api-version 处有终点,如 https://msdn.microsoft.com/library/azure/ad/graph/api/functions-and-actions#isMemberOf

中所述