azure - 当范围不是 Azure AD 中的图形 API 时,如何添加自定义声明并检索相同的声明作为 access_token 的一部分?

标签 azure active-directory azure-active-directory microsoft-graph-api azure-web-app-service

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

发布 https://login.microsoftonline.com/{目录(租户)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_{Azure AD 应用程序的客户端 ID}_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 是代表您的第三方应用程序的企业应用程序。在您的情况下,它是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 Portal -> Azure Active Directory -> 应用注册 -> 找到您的第三方 Azure AD 应用找到 servicePrincipal -> 概述 -> 单击其关联服务主体的名称。

enter image description here

enter image description here

现在返回到第三方 Azure AD 应用程序的 list 文件。将 acceptMappedClaims 设置为 true,并将 accessTokenAcceptedVersion 设置为 2。

enter image description here

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

enter image description here

关于azure - 当范围不是 Azure AD 中的图形 API 时,如何添加自定义声明并检索相同的声明作为 access_token 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63483491/

相关文章:

azure - 如何通过 Azure 门户查找虚拟机上的 Windows 版本

python - 单个Python脚本的Azure函数部署以及Azure Functions中requirements.txt的安装过程

node.js - 身份验证问题 - 从 azure 函数调用 Azure REST API

c# - 将 login_hint 与 OpenID 结合使用

azure - Azure 逻辑应用中的随机列表

java - 为任何给定大小的文件形成相等长度的 Base64 字符串

active-directory - 通过 ldapmodify 在 AD 中更改密码时违反 LDAP 约束

c# - 使用 C# 修改 LDAP PhoneNumber 和 IPPhone 属性

azure - 是否可以在不为应用程序创建特定范围的情况下设置基于 token 的授权?

ios - 如果我的应用程序使用 AD B2C,是否需要 Apple 登录?