简体   繁体   English

多个WebApi端点的Azure AD承载令牌?

[英]Azure AD Bearer Token For Multiple WebApi Endpoints?

We're using Angular, .Net WebApi, and Azure to build out several applications. 我们使用Angular,.Net WebApi和Azure来构建多个应用程序。 And what we've been doing is securing the applications through Azure AD via the implicit oAuth2/OIDC grant flow. 我们一直在做的是通过隐式oAuth2 / OIDC授权流程通过Azure AD保护应用程序。

Things are working well, but we have had a simple architecture so far. 事情进展顺利,但到目前为止我们已经有了一个简单的架构。 IE 'Front End App1' -> 'WebApi App1' IE'前端App1' - >'WebApi App1'

When we request the token to AAD we send the resource (Application Id for 'WebApi App1') in the token request. 当我们向AAD请求令牌时,我们在令牌请求中发送资源(“WebApi App1”的应用程序ID)。 It seems this is a required property in the token request. 看来这是令牌请求中的必需属性。 How does this scale out? 这是如何扩大的?

Say we have a situation where 'Front End App1' needs to talk to 'WebApi App1' and 'WebApi App2'. 假设我们遇到'前端App1'需要与'WebApi App1'和'WebApi App2'交谈的情况。 Do we need to make multiple token requests? 我们需要发出多个令牌请求吗? What do people do in these situations in Azure? 在Azure中,人们在这些情况下做了什么? It seems wonky to tightly couple a bearer token to one resource. 将承载令牌紧密地耦合到一个资源似乎很难。

It seems that another approach is that I can have both the api apps configured to validate tokens for the same Azure tenant and application Id. 似乎另一种方法是我可以将api应用程序配置为验证相同Azure租户和应用程序ID的令牌。 That way the token is good for either apps, but that isn't very flexible either as that would mean all tokens for either app are good for the other... 这样,令牌对任何一个应用程序都有好处,但这也不是很灵活,因为这意味着任何一个应用程序的所有令牌都有利于另一个...

You have two options: 您有两种选择:

  1. Get two tokens 获得两个令牌
  2. Use the same app id for both APIs 对两个API使用相同的应用程序ID

Getting a token per API is the normal approach. 获取每个API的令牌是正常的方法。

A bearer token in Azure AD is always only valid for one API. Azure AD中的承载令牌始终仅对一个API有效。 It can contain scopes/roles of the calling user/app on that API, and those values are simple strings defined in the app manifest, eg User.Read. 它可以包含该API上的调用用户/应用程序的范围/角色,这些值是应用程序清单中定义的简单字符串,例如User.Read。 Those values can overlap between APIs, and thus we cannot have a token valid for two APIs. 这些值可以在API之间重叠,因此我们不能拥有对两个API有效的令牌。

Problem Statement: How to call multiple WebApi using same token from UI application. 问题陈述:如何使用UI应用程序中的相同令牌调用多个WebApi。

My Solution: 我的解决方案

Let there be two Web-Api, WebApi01 and WebApi02. 让我们有两个Web-Api,WebApi01和WebApi02。 In Azure AD, instead of registering two Applications, I registered just one Application, say MasterAPI. 在Azure AD中,我只注册了一个应用程序,而不是注册两个应用程序,比如MasterAPI。 Also, the client application ie UI app has to be registered, as usual. 此外,客户端应用程序即UI应用程序必须像往常一样进行注册。

The MasterAPI is not an actual application. MasterAPI不是实际应用程序。 So, there might not be any redirect URL, neither we need any. 因此,可能没有任何重定向网址,也不需要任何重定向网址。 In this API, we declare two scopes, to start with, for each of the WebAPI (WebAPI01 & WebAPI02). 在此API中,我们为每个WebAPI(WebAPI01和WebAPI02)声明两个作用域。 I usually follow a nomenclature like App.Feature.Verb etc. So, the scopes names will be something like Master.WebAPI01.Read and Master.WebAPI02.Read . 我通常遵循App.Feature.Verb等命名法。因此,范围名称将类似于Master.WebAPI01.ReadMaster.WebAPI02.Read

They will be exposed like api://MasterAPI-Application-Id//Master.WebAPI01.Read and api://MasterAPI-Application-Id//Master.WebAPI02.Read in azure application registration page. 它们将像api://MasterAPI-Application-Id//Master.WebAPI01.Readapi://MasterAPI-Application-Id//Master.WebAPI02.Read在azure应用程序注册页面中公开

During Access-Token request, I pass these two scopes to Azure AD endpoint. 在Access-Token请求期间,我将这两个范围传递给Azure AD端点。 The response I get contains the scope claim (scp) with the value "Master.WebAPI01.Read Master.WebAPI02.Read". 我得到的响应包含范围声明(scp),其值为“Master.WebAPI01.Read Master.WebAPI02.Read”。

"scp":"Master.WebAPI01.Read Master.WebAPI02.Read" “scp”:“Master.WebAPI01.Read Master.WebAPI02.Read”

This way I am telling to system that the trusted Client-UI application has a token with accessibility to two features. 这样我就告诉系统,可信的Client-UI应用程序有一个可以访问两个功能的令牌。 Simply put, whoever can log into the Client Application, will have access to both the features. 简而言之,无论谁登录客户端应用程序,都可以访问这两个功能。 An Authenticated user is now authorized to use two features. 经过身份验证的用户现在有权使用两个功能。

This can be further restricted. 这可以进一步限制。 Say, I, the admin, should have control over who can access what. 说,我,管理员,应该控制谁可以访问什么。 For that, I had to implement Role based access control (RBAC). 为此,我必须实现基于角色的访问控制(RBAC)。 I defined Custom made Application roles in the MasterAPI. 我在MasterAPI中定义了自定义应用程序角色。 This can be done by manipulating manifest. 这可以通过操纵清单来完成。

Now, when I receive access token, I get the roles claim with the value set. 现在,当我收到访问令牌时,我获得了带有值集的角色声明。 The value depends on what roles are assigned to the user. 该值取决于为用户分配的角色。

"roles": [ "Access_WebAPI01", "Access_WebAPI02" ], “roles”:[“Access_WebAPI01”,“Access_WebAPI02”],

The token is passed to the resource WebAPI (in this case WebAPI01 or WebAPI02) via header.The receiver WebAPI then parses the header token to check whether required role exists or not. 令牌通过头传递给资源WebAPI(在本例中为WebAPI01或WebAPI02)。接收器WebAPI然后解析头令牌以检查是否存在所需角色。

So, in nutshell, Scope and RBAC combination works for me. 因此,简而言之,Scope和RBAC组合对我有用。 And I do not have to hit database for Authentication and Authorization. 而且我不需要为数据库进行身份验证和授权。

In case a new feature is added, say, WebAPI03. 如果添加了新功能,例如WebAPI03。 All I need to do is Set up the scope in MasterAPI. 我需要做的就是在MasterAPI中设置范围。 and Either use existing roles or assign a new role to the user. 并使用现有角色或为用户分配新角色。

PS: Why MasterAPI? PS:为什么选择MasterAPI? In Azure AD, all the scopes has to be from one resource only. 在Azure AD中,所有范围必须仅来自一个资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM