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

fae0ux8s  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(178)

我们已在Azure中为SPA配置了应用注册,用于身份验证代码流。
我们在可选报销申请下添加了电子邮件,如下所示:

清单文件配置如下:

{
    "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浏览器使用提供的以下作用域启动身份验证:

但是,我一辈子也弄不明白为什么email声明没有出现在access_token中

mzillmmw

mzillmmw1#

请参阅v1.0和v2.0可选声明集。
当向访问令牌添加声明时,声明应用于为应用(web API)请求的访问令牌,而不是应用请求的声明。
这意味着您的email声明适用于您调用自己的Web API而不是调用Microsoft Graph API的场景。
您可以从Protected web API: App registration查看详细信息。
你应该在代表Web API的Azure AD应用中配置email可选声明,而不是在代表客户端的Azure AD应用中配置email可选声明。这样,当你请求该API的访问令牌时,email声明将存在于访问令牌中。
在请求中设置scope=api://{app id of the AAD app which represents the web api}/.default openid而不是scope=http://graph.microsoft.com/.default openid
因此,对于调用Microsoft Graph API,您不能直接使用内置的email可选声明。您需要通过调用Microsoft Graph GET https://graph.microsoft.com/v1.0/me/或在访问令牌中使用另一个声明upn来查询电子邮件。

相关问题