【问题标题】:Role/Group based Authorization using ADAL.js and ASP.NET Web API使用 ADAL.js 和 ASP.NET Web API 的基于角色/组的授权
【发布时间】:2018-02-21 20:16:58
【问题描述】:

我们计划为前端 Angular-2 和后端 ASP.NET Web API 应用程序实现基于角色的安全性。我们在 ADAL.js 的帮助下进行身份验证过程并将令牌存储在本地存储中。我们还实现了here 所示的方法,即调用 Graphi API 并让用户组将它们填充到 Claims 中。

我的问题是:无论如何,我们可以将角色声明从服务器添加到驻留在本地存储中的 BearerToken。或者有没有更好的方法来解决这个问题。

【问题讨论】:

    标签: azure-active-directory azure-ad-graph-api adal.js


    【解决方案1】:

    提到的代码示例根据组分配角色。如果你有 Azure AD 基础版,它支持直接将角色分配给用户/组。

    我的问题是:无论如何,我们可以将角色声明从服务器添加到驻留在本地存储中的 BearerToken。或者有没有更好的方法来解决这个问题。

    是的,这是可能的。要发出角色声明,我们需要先分配用户以将角色分配给用户或组。然后当用户获取令牌时,Azure AD 会在令牌中发出相关的角色声明。

    您可以参考here 中使用角色声明的代码示例。

    你也可能对groups claim developing感兴趣。

    【讨论】:

    • 我们基本上想要实现的是将我们的组映射到角色并进行基于角色的授权,而无需使用 Azure Active Directory 高级版。无论如何,我们可以使用 ADAL.js 获取令牌,然后在服务器端调用 Microsoft Graph API 并将组插入令牌中?
    • 不,这是不可能的。因为令牌是从 Azure AD 颁发/签名的。出于安全考虑,我们无法更改令牌。
    【解决方案2】:

    好的,我为此苦苦挣扎了一段时间,我相信我已经弄清楚了。

    首先, 在 Azure AD 中,将您的 WebApi 应用程序设置为应用程序类型为 Web 应用程序/API。 转到清单文件并添加您的角色,例如

    [
        {
          "allowedMemberTypes": [
            "User"
          ],
          "displayName": "Reviewer",
          "id": "0238c2bb-9857-4d07-b760-a47ec621d57a",
          "isEnabled": true,
          "description": "Reviewer only have the ability to view tasks and their statuses.",
          "value": "reviewer"
        },
        {
          "allowedMemberTypes": [
            "User"
          ],
          "displayName": "Approver",
          "id": "000018cb-19e3-4f89-bf99-5d7acf30773b",
          "isEnabled": true,
          "description": "Approvers have the ability to change the status of tasks.",
          "value": "approver"
        }
      ]
    

    然后将 Client 应用程序创建为 Application 类型为 Native 应用程序,并为您在上面添加的服务添加所需的权限。

    在 SPA Angular 应用程序中添加类似这样的内容

        var endPoints = {
            // "https://localhost:44386/" is the API URL
            // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" is the Service Application ID
            "https://localhost:44386/": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
        };
        adalAuthenticationServiceProvider.init({
            instance: "https://login.microsoftonline.com/",
            // tenant is your tenant name (something like below)
            tenant: "{NAME}.onmicrosoft.com",
            // this is the Native app application ID (ClientID) you registered
            clientId: "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
            extraQueryParameter: "nux=1",
            endpoints: endPoints
        }, $httpProvider);
    }
    

    ]);

    然后,您需要在您的 startup.cs 中设置 Service App,如下所示:

         app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                 new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                 {
                     TokenValidationParameters = new TokenValidationParameters
                     {
                    /* "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" is the Service Application ID. (Same as you registered in the client app above)*/
                         ValidAudience = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                         RoleClaimType = "roles"
                     },
                   /*enant is your tenant name (same as you registered in client app above)*/
                     Tenant = "{NAME}.onmicrosoft.com"
                 });
    

    最后,您需要转到 Azure 活动目录 => 企业应用程序 => 所有应用程序 => 选择您的 webAPI 服务 => 用户和组 => 然后将用户分配给角色。

    当您通过客户端应用程序登录以进行身份​​验证并调用 webapi 时,这一切都完成后,adal.js 和 ada-angular.js 将放置包含角色的正确不记名令牌

    【讨论】:

      【解决方案3】:

      很高兴学习这种方法。

      Ted,感谢您分享您的解决方案!

      对于那些不熟悉操作 Azure AD 清单文件的人。以下是一个很好的资源。 https://thinkthencode.wordpress.com/2016/04/24/azure-ad-using-app-roles-for-authorization/

      "appRoles": [
              {
        "allowedMemberTypes": [
          "User"
        ],
        "displayName": "Reviewer",
        "id": "0238c2bb-9857-4d07-b760-a47ec621d57a",
        "isEnabled": true,
        "description": "Reviewer only have the ability to view tasks and their statuses.",
        "value": "reviewer"
      },
      {
        "allowedMemberTypes": [
          "User"
        ],
        "displayName": "Approver",
        "id": "000018cb-19e3-4f89-bf99-5d7acf30773b",
        "isEnabled": true,
        "description": "Approvers have the ability to change the status of tasks.",
        "value": "approver"
      }
          ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-18
        • 2018-06-14
        • 2020-05-18
        • 2021-03-12
        • 2020-05-19
        • 2019-05-27
        • 2021-11-26
        • 1970-01-01
        相关资源
        最近更新 更多