简体   繁体   English

Azure AD B2C单页应用程序角色

[英]Azure AD B2C Single Page App Roles

We are trying to integrate Azure AD B2C into a SPA. 我们正在尝试将Azure AD B2C集成到SPA中。 We want to include Roles in the ticket so that we can use AuthorizeRoles & IsInRole in the api. 我们希望在票证中包含“角色”,以便我们可以在api中使用AuthorizeRoles和IsInRole。 We have looked at a couple of examples. 我们看了几个例子。

Example 1 例子1

Example 2 例子2

The first example isn't a SPA and doesn't include roles. 第一个示例不是SPA,也不包含角色。 It is accepted that including membership & groups in the ticket using Azure AD B2C isn't supported as per below link. 根据下面的链接,不支持使用Azure AD B2C在票证中包括成员资格和组。

Azure AD B2C Group Membership Feature Feedback Azure AD B2C组成员资格功能反馈

The workaround as suggested above seems to be to use the "OnAuthorizationCodeReceived" event as per below to inject/add your own Role claims to the ticket. 上面建议的解决方法似乎是按照下面的方式使用“ OnAuthorizationCodeReceived”事件来向故障单注入/添加您自己的角色声明。

Workaround 解决方法

The issue we have is that we are using a SPA so we need to follow example 2, we also need to be able to add our own manged roles into the ticket which isn't a supported feature but Microsoft have said there is a workaround as shown. 我们遇到的问题是我们正在使用SPA,因此我们需要遵循示例2,我们还需要能够将自己的管理角色添加到票证中,这是不受支持的功能,但是Microsoft表示存在一种解决方法,因为如图所示。 The workaround however doesn't work with MSAL.js as in example 2. 但是,该解决方法不适用于示例2中的MSAL.js。

How can we include our own managed Roles into the ticket using the MSAL.js library so we can integrate Azure AD B2C into our SPA enabling us to use AuthorizeRoles & IsInRole in the api? 我们如何使用MSAL.js库将自己的托管角色包含在故障单中,以便将Azure AD B2C集成到SPA中,从而使我们能够在api中使用AuthorizeRoles和IsInRole?

A few things first, you mentioned you are using the MSAL.js library and this means the v2 endpoint. 首先,您提到要使用MSAL.js库,这意味着v2端点。 Currently (as of 05/16/2018), the v2 endpoint has limitations on roles and groups, see v2 limitations where it states: 当前(截至2018年5月16日),v2端点对角色和组具有限制,请参阅v2限制 ,其中指出:

The v2.0 endpoint does not support issuing role or group claims in ID tokens. v2.0端点不支持ID令牌中的发布角色或组声明。

ID tokens are used in the implicit flow with the v2 endpoint, see here Azure AD v2 Spa Guided Setup and read about half way down under More Information ID 令牌与V2端点隐式流程中使用,在这里看到的Azure的广告V2温泉设置向导和阅读有关中途下更多信息下跌

Bottom line is to be absolutely sure the v2 endpoint (and MSAL libraries) can support your requirements. 底线是绝对要确保v2端点(和MSAL库)可以满足您的要求。

For myself, we ended up going with the v1 endpoint and ADAL libraries in part because of limitations like this. 就我自己而言,我们最终选择了v1端点和ADAL库,部分原因是因为这样的限制。 But here are some examples of using roles in code. 但是,这里有一些在代码中使用角色的示例。 Note that these repos are fairly new and I'm still building out the documentation. 请注意,这些存储库是相当新的,我仍在构建文档。 There are two repos, one an stand alone angularjs ui project and another is a set of APIs (they are demos I used at a codecamp presentation). 有两个资源库,一个是独立的angularjs ui项目,另一个是一组API(它们是我在codecamp演示中使用的演示)。 Read on below about roles in AAD. 在下面阅读有关AAD中角色的信息。 Only the UI example uses the ADAL libraries (note: the ADAL and MSAL libraries are about managing the tokens in the clients and are not the libraries used for locking down the back ends). 仅UI示例使用ADAL库(请注意:ADAL和MSAL库是关于在客户端中管理令牌的,而不是用于锁定后端的库)。

APIs: https://github.com/BgRva/aad_adal_api_dn_std/tree/Step_C API: https//github.com/BgRva/aad_adal_api_dn_std/tree/Step_C

UI: https://github.com/BgRva/aad_adal_ui_ng_js/tree/Step_C 用户界面: https//github.com/BgRva/aad_adal_ui_ng_js/tree/Step_C

Some notes about Roles in AAD: 有关AAD中角色的一些说明:

  • Roles are application specific , so in the examples above, the same roles have to be registered for all the applications that will use them (this includes the role Id as well) 角色是特定应用程序的 ,因此在上面的示例中,必须为将要使用它们的所有应用程序注册相同的角色(这也包括角色ID)
  • You can give multiple roles to a user in AAD, you just need to add them multiple times to that application with a different role selected. 您可以在AAD中为用户分配多个角色,只需将它们添加到具有不同选择角色的该应用程序中多次即可。
    • Manually adding users to roles will not scale and requires lots of clicks in the portal. 手动将用户添加到角色将无法扩展,并且需要在门户中进行大量单击。 It can be easier managed with groups but only at the AAD Premium Level 2 do you get this benefit 通过组可以更轻松地进行管理,但是只有在AAD Premium Level 2上您才能获得此好处
    • Supposedly there is a way to programmatically add roles but I have not found anything about it 大概有一种方法可以以编程方式添加角色,但我还没有找到任何相关信息

Cheers 干杯

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

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