我想在 React Web 客户端中使用 MSAL 检索的访问令牌中包含组 ID。我通过以下方式检索访问令牌:
const accessToken = await instance.acquireTokenSilent({
scopes: [
"https://graph.windows.net/Group.Read.All",
"https://graph.windows.net/User.Read",
],
account: accounts[0],
})
).accessToken;
我已在 Azure 中授予应用程序注册
Microsoft Graph API Group.Read.All (delegated)
权限。我还在 groups
中添加了 Token configuration section
作为可选声明。因此应用程序清单现在具有以下属性:
{
"accessTokenAcceptedVersion": 2,
(...)
"groupMembershipClaims": "SecurityGroup",
(...)
"optionalClaims": {
"accessToken": [
{
"name": "groups",
"source": null,
"essential": false,
"additionalProperties": []
}
],
},
(...)
}
我成功检索了具有有效签名的访问令牌。访问令牌还包括以下声明:
{
"scp": "Group.Read.All User.Read"
}
,但缺少预期的
groups
声明,该声明应包含组 ID 列表。我哪里配置不正确?
使用 MSAL 检索的
id_token
似乎包含组 ID,但不包含访问令牌。
当您定义可选声明时,您所说的是“在该 API 的访问令牌中包含组 ID”。 您将获得 Microsoft Graph API 的访问令牌,而不是您的 API。 而且他们没有配置要包含的组 ID。
您需要在应用程序注册的“公开 API”选项卡中定义一个范围,并在获取访问令牌时使用该范围。 该令牌将包含组 ID。