【问题标题】:Angularjs Adal and additional claims or properties for AuthorizationAngularjs Adal 和其他授权声明或属性
【发布时间】:2018-02-21 22:02:31
【问题描述】:

场景是带有 c# WebApi 的 Angularjs 1.6.5 应用程序。使用angular-adal.jsAAD 进行身份验证。到目前为止,一切正常,因为用户可以通过 AAD 登录并且 WebApi 接受令牌。

对于此特定应用,角色位于 WebApi 有权访问的外部应用中。我已经能够使用WindowsAzureActiveDirectoryBearerAuthenticationOptionsConfigureOAuth(IAppBuilder app) 中的以下代码添加角色声明(从外部应用程序获取它们之后):

app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
   TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
   {
       ValidAudience = clientId
   },
   //Audience = ConfigurationManager.AppSettings["ida:ClientID"],
   Tenant = tenant,


   Provider = new OAuthBearerAuthenticationProvider
   {
       OnValidateIdentity = async context =>
     {
         // Retrieve user JWT token from request.
         var authorizationHeader = context.Request.Headers["Authorization"];
         var userJwtToken = authorizationHeader.Substring("Bearer ".Length).Trim();

         // Get current user identity from authentication ticket.
         var authenticationTicket = context.Ticket;
         var identity = authenticationTicket.Identity;

         if (identity.FindFirst(System.Security.Claims.ClaimTypes.Role) == null)
         {
             var user = identity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn").Value;

             Cis.bll.Xrm.bllSystemUserRoles bllSystemUserRoles = new Cis.bll.Xrm.bllSystemUserRoles();
             var su = bllSystemUserRoles.getByEmail(user);
             //var roleClaim = new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, su.stringRoles);
             foreach (var item in su.Roles)
             {
                 identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, item.xrmName));
             }
         }
     }
   }
});

因此,对于 Angularjs 对 API 执行的每个 httpRequest,前面的函数都会为用户查找角色并添加角色声明。通过这个实现,我可以在 Controller 方法中使用 AuthorizeAttribute,将访问权限限制为仅限某些角色,如下所示:

[CustomAuthorize(Constants.Roles.resourcesAdministrator)]

我发现这种方式非常低效,因为对于每个 httpRequest,API 都必须从数据库中获取用户的角色(或实现的任何持久化方式)。

我想要做的是只读取一次用户角色,然后能够在 API 中使用它们来处理每个后续请求。在我们收到 AAD 的令牌后,有没有办法将声明添加到令牌中?

顺便说一句,我可以为每个模型添加一个 Roles 属性,或者类似的东西,但这不是我想要的。

如果您有任何其他想法或建议,我们将不胜感激。

问候

【问题讨论】:

  • angular 标签适用于2+ angularjs 标签适用于1.x
  • 感谢@Kuncevic 的更新。当我们使用 Angular 1.x 时,我会记住这一点。

标签: angularjs token claims adal.js


【解决方案1】:

令牌自发行以来无法修改。而且由于角色存储在其他应用程序中,我认为不查询数据库就无法获取角色。

在这种情况下,我们可以通过 Azure AD 应用程序角色和角色声明来管理角色。然后它将在 id_token 中发出roles 声明。

例如,我们可以像下面这样修改应用的清单:

"appRoles": [
    {
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Writer",
      "id": "d1c2ade8-98f8-45fd-aa4a-6d06b947c66f",
      "isEnabled": true,
      "description": "Writers Have the ability to create tasks.",
      "value": "Writer"
    },
    {
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Observer",
      "id": "fcac0bdb-e45d-4cfc-9733-fbea156da358",
      "isEnabled": true,
      "description": "Observers only have the ability to view tasks and their statuses.",
      "value": "Observer"
    },
    {
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Approver",
      "id": "fc803414-3c61-4ebc-a5e5-cd1675c14bbb",
      "isEnabled": true,
      "description": "Approvers have the ability to change the status of tasks.",
      "value": "Approver"
    },
    {
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Admin",
      "id": "81e10148-16a8-432a-b86d-ef620c3e48ef",
      "isEnabled": true,
      "description": "Admins can manage roles and perform all task actions.",
      "value": "Admin"
    }
  ],

并通过 Azure 门户通过应用程序将角色分配给用户,如下图所示:

然后我们可以得到如下请求的id_token(隐式授权流),角色应该在token中。我们可以使用这个令牌调用 Web API。

Get:https://login.microsoftonline.com/{tenant}/oauth2/authorize?response_type=id_token&client_id={clientId}&redirect_uri={redirect_uri}&nonce={nonce}

id_token 示例:

【讨论】:

  • 感谢您抽出时间来回答 Fei,因此,我应该将外部应用程序中的角色与 AAD 角色同步。这样,两个系统共享相同的角色,我将获得令牌中的角色。对吗?
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2013-07-18
  • 2015-06-28
相关资源
最近更新 更多