当范围不是Azure AD中的Graph API时,如何添加自定义声明并作为access_token的一部分进行检索?

问题描述

我已经创建了一个Azure AD Web应用程序。现在,我使用以下API获取了access_token,

开机自检 https://login.microsoftonline.com/{Directory (tenant) ID }/oauth2/v2.0/token

password:pass 
client_id:id
resource:https://graph.microsoft.com 
grant_type:password 
client_secret:secret 
sername:userName 
scope: https://rbsessence.onmicrosoft.com/0a7c94a0-0c4e-4f95-ba06-XXXX/.default

响应看起来像

"token_type": "Bearer","scope": "https://rbsessence.onmicrosoft.com/0a7c94a0-0c4e-4f95-ba06-XXXXX/myTestRole https://rbsessence.onmicrosoft.com/0a7c94a0-0c4e-4f95-ba06-XXXXXX/user_impersonation https://rbsessence.onmicrosoft.com/0a7c94a0-0c4e-4f95-ba06-XXXXX/.default","expires_in": 3599,"ext_expires_in": 3599,"access_token": "acesstoken"

现在,我将access_token传递给配置了相同Azure AD客户端的第三方应用程序。现在,第三方希望将名称"policy":"readwrite"自定义声明作为access_token的一部分进行传递。我怎么能达到同样的目的?

解决方法

请参考以下步骤(您可以在Microsoft Graph Explorer中执行Microsoft Graph操作以节省时间。)

Create an extensionProperty(您可以在此处使用新创建的Azure AD应用程序):

Post https://graph.microsoft.com/v1.0/applications/{object id of the Azure AD application}/extensionProperties

{"name":"policy","dataType":"string","targetObjects":["User"]}

它将生成名为extension_{client id of the Azure AD application}_policy的扩展属性。

第二,您可以更新帐户的扩展程序属性:

Patch https://graph.microsoft.com/v1.0/me

{"extension_6d8190fbf1fe4bc38a5a145520221989_policy":"readwrite"}

然后create a claimsMappingPolicy

Post https://graph.microsoft.com/v1.0/policies/claimsMappingPolicies

{"definition":["{\"ClaimsMappingPolicy\":{\"Version\":1,\"IncludeBasicClaimSet\":\"true\",\"ClaimsSchema\": [{\"Source\":\"user\",\"ExtensionID\":\"extension_6d8190fbf1fe4bc38a5a145520221989_policy\",\"JwtClaimType\":\"policy\"}]}}"],"displayName":"ExtraClaimsAllen1Example","isOrganizationDefault":true}

Assign the claimsMappingPolicy到servicePrincipal。 请注意,这里的servicePrincipal是代表您的第三方应用程序的企业应用程序。您的情况是0a7c94a0-0c4e-4f95-ba06-XXXX

Post https://graph.microsoft.com/v1.0/servicePrincipals/{obeject id of the servicePrincipal which represents your third party application}/claimsMappingPolicies/$ref

{"@odata.id":"https://graph.microsoft.com/v1.0/policies/claimsMappingPolicies/{policy id from the previous step}"}

您可以从 Azure门户网站-> Azure Active Directory -> 应用程序注册->查找第三方Azure AD应用程序找到servicePrincipal -> 概述->单击其关联的服务主体的名称。

enter image description here

enter image description here

现在返回第三方Azure AD应用的清单文件。将acceptMappedClaims设置为true,将accessTokenAcceptedVersion设置为2。

enter image description here

然后,当我们通过ROPC授予流程为第三方应用程序请求访问令牌时,我们可以获得自定义声明。

enter image description here