azure - 如何在Azure Ad中实现代表流?

标签 azure asp.net-core authentication azure-active-directory jwt

网络核心应用程序。我在 azure ad 中注册了三个应用程序。

  1. React SPA 应用
  2. Web API 网关应用
  3. 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 .

enter image description here

代表流程的步骤:

  1. 将 Web API Gateway 应用的 API 权限添加到 SPA 应用。

enter image description here

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/

    相关文章:

    asp.net-core - ASP.NET CORE MVC 可以托管在 Xamarin App(Android) 中吗?

    c# - 身份服务器 4 : How so you include additional claims and get the value in an API controller?

    c# - ASP.Net-Core 中的自定义身份验证

    apache - 如何在子目录 (.htaccess) 中使用不同的密码

    azure - POST JSON 将事件记录到 Application Insights 中

    java - Azure 服务总线中的 ServiceBusSenderClient 和 ServiceBusSenderAsyncClient 有什么区别?

    asp.net-core - ASP .NET Core Entity Framework 的范围

    mysql - 使用 LAMP 堆栈的 IOS 身份验证

    android - 在android中玩iis流畅流(.ism)的技巧

    .net - Windows Server 重新启动后未启动