azure - 无法在 Azure AD 的 access_token 中获取电子邮件声明

标签 azure authentication azure-active-directory claims

我们已在 Azure 中为 SPA 配置了应用程序注册,用于身份验证代码流程。

我们在可选声明下添加了电子邮件,如下所示: enter image description here

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
}

我们已将电子邮件添加到权限中: enter image description here

最后,在客户端,我使用 MSAL 浏览器通过提供的以下范围启动身份验证:

enter image description here

但是,我一生都无法弄清楚为什么电子邮件声明没有出现在 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/

相关文章:

azure - 使用 Microsoft Graph 扩展 MemberOf 不会填充组

azure - 如何使用 Liquid 在 Azure 逻辑应用程序中从 xml 转换为 json

visual-studio - Azure 函数在 function.json 的本地中缺少绑定(bind)

azure - "az network vnet peering create"结果为 "BadRequestError: Cannot parse the request."

Azure Active Directory 撤销给定 aad app-id 的一组用户 session

java - Google Cloud Endpoints 限制...任何建议的解决方案?

php - 使用 Cookie 进行 PHP 身份验证的最佳做法是什么?

编辑 list 以启用 availableToOtherTenants 后出现 Azure AD 错误

authentication - JAAS LoginModule login() 和 commit() 成功,但用户未登录

asp.net - 使用 Azure AD 进行 SSO 时,Microsoft 如何使用 Post 方法重定向到客户端应用程序