我们已在 Azure 中为 SPA 配置了应用程序注册,用于身份验证代码流程。
list 文件配置如下:
{
"id": "<redacted>",
"acceptMappedClaims": true,
"accessTokenAcceptedVersion": 1,
"addIns": [],
"allowPublicClient": null,
"appId": "<redacted>",
"appRoles": [],
"oauth2AllowUrlPathMatching": false,
"createdDateTime": "2020-12-03T10:30:07Z",
"disabledByMicrosoftStatus": null,
"groupMembershipClaims": "None",
"identifierUris": [],
"informationalUrls": {
"termsOfService": null,
"support": null,
"privacy": null,
"marketing": null
},
"keyCredentials": [],
"knownClientApplications": [],
"logoUrl": null,
"logoutUrl": null,
"name": "<redacted>",
"oauth2AllowIdTokenImplicitFlow": false,
"oauth2AllowImplicitFlow": false,
"oauth2Permissions": [],
"oauth2RequirePostResponse": false,
"optionalClaims": {
"idToken": [],
"accessToken": [
{
"name": "email",
"source": null,
"essential": false,
"additionalProperties": []
}
],
"saml2Token": []
},
"orgRestrictions": [],
"parentalControlSettings": {
"countriesBlockedForMinors": [],
"legalAgeGroupRule": "Allow"
},
"passwordCredentials": [],
"preAuthorizedApplications": [],
"publisherDomain": "<redacted>",
"replyUrlsWithType": [
{
"url": "https://localhost:44338",
"type": "Spa"
}
],
"requiredResourceAccess": [
{
"resourceAppId": "<redacted>",
"resourceAccess": [
{
"id": "<redacted>",
"type": "Scope"
},
{
"id": "<redacted>",
"type": "Scope"
},
{
"id": "<redacted>",
"type": "Scope"
}
]
},
{
"resourceAppId": "<redacted>",
"resourceAccess": [
{
"id": "<redacted>",
"type": "Scope"
}
]
}
],
"samlMetadataUrl": null,
"signInUrl": null,
"signInAudience": "AzureADMyOrg",
"tags": [],
"tokenEncryptionKeyId": null
}
最后,在客户端,我使用 MSAL 浏览器通过提供的以下范围启动身份验证:
但是,我一生都无法弄清楚为什么电子邮件声明没有出现在 access_token 中
最佳答案
请参阅v1.0 and v2.0 optional claims set .
When adding claims to the access token, the claims apply to access tokens requested for the application (a web API), not claims requested by the application.
这意味着您email
声明适用于您调用自己的Web API的场景,而不是调用Microsoft Graph API。
您可以通过Protected web API: App registration查看详细信息.
您应该在代表 Web API 的 Azure AD 应用程序(而不是代表客户端的 Azure AD 应用程序)中配置电子邮件
可选声明。然后,当您请求该 API 的访问 token 时,电子邮件
声明将存在于访问 token 中。
设置 scope=api://{代表 Web api 的 AAD 应用的应用 ID}/.default openid
而不是 scope=http://graph.microsoft.com/.请求中的默认openid
。
因此,在调用 Microsoft Graph API 时,您不能直接使用内置的 email
可选声明。您需要通过调用 Microsoft Graph GET https://graph.microsoft.com/v1.0/me/
或在访问 token 中使用另一个声明 upn
来查询电子邮件。
关于azure - 无法在 Azure AD 的 access_token 中获取电子邮件声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65128383/