网络核心应用程序。我在 azure ad 中注册了三个应用程序。
- React SPA 应用
- Web API 网关应用
- protected API 应用
每当用户登录 SPA 应用程序时,都会使用隐式流重定向到 Azure AD 以获取访问 token 。 我已收到如下访问 token ,并用于从网关层访问 api
{
"aud": "7851c317b-87e7-4cb3-95f0-37cb52b6f873",
"family_name": "alex",
"given_name": "fernandes",
"hasgroups": "true",
"roles": [
"Admin"
]
}
在上面的 token aud 中,是 spa 应用程序的客户端 ID 以及我添加的其他一些详细信息,但不是全部详细信息。
现在我想从 Web API 网关应用程序调用 protected API 应用程序
我正在尝试在 postman 中使用示例 http 请求,如下所示。
请求 - https://login.microsoftonline.com/45fgh-f30d-4596-gt67-7045b338485a/oauth2/v2.0/token
正文
{
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"
"client_id": "" //unknown client id of SPA or API GateWay or Protected API
"client_secret": "" //unknown client id of SPA or API GateWay or Protected API
"assertion": "Above token"
"scope":"" //unknown
"requested_token_use" : "on_behalf_of"
}
除了上述困惑之外,我只是想知道 azure ad 中是否需要任何配置才能为 protected API 生成 token 。另一件事是为访问 protected API 生成的 token ,该 token 是否具有与 SPA 生成的上述 token 相同的用户详细信息和角色详细信息?
有人可以帮我配置代表流程吗?我在这里努力完成这件事。任何帮助,将不胜感激。谢谢
最佳答案
如下图所示,Client App 表示 React SPA 应用程序,Web API 1 表示 Web API Gateway 应用程序,Web API 2 表示 protected API 应用程序。更多详情请参见this blog .
代表流程的步骤:
- 将 Web API Gateway 应用的 API 权限添加到 SPA 应用。
GET https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize
?scope={permission for Web API Gateway application like api://1108f6-xxxxxxx-9f622/test} openid
&redirect_uri={redirect_uri of SPA application}
&nonce=123
&client_id={client-id of SPA application}
&response_type=id_token token
- 将 protected API 应用程序的 API 权限添加到 Web API 网关应用程序。
调用官方的Microsoft Graph API document ,因此这一步的范围是“user.read”。
POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&client_id={client_id of Web API Gateway application}
&client_secret={client_secret}
&assertion={access token from previous step}
&scope={permission for Protected API application}
&requested_token_use=on_behalf_of
- 使用访问 token 调用 protected API 应用
关于azure - 如何在Azure Ad中实现代表流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66659229/