Azure Active Directory用户角色和授权

时间:2018-04-16 11:55:25

标签: angular azure azure-active-directory asp.net-core-2.0 user-roles

我在Azure上安装了应用程序,在前端安装了Angular 4,在后端安装了.net core 2.0。 我想要实现的是:将角色添加到添加到我的Azure Active Directory的用户。 身份验证已实施且运行良好。我使用ADAL,每次请求都会发送我的持票令牌。

  1. 这些是我在azure portal上的清单中定义的app角色:

    "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"
    }
    

  2. 审批者角色已分配给所需用户。

  3. 我向后端(.net core 2.0 web api)发送请求,其中我有[Authorize]属性,我检查用户声明 User.Claims.ToList()然后我收到用户是批准人:

  4.   

    [15] {http://schemas.microsoft.com/ws/2008/06/identity/claims/role:   审批}

    那太棒了!

    1. 现在我在startup.cs中添加策略

      services.AddAuthorization(options =>
      {
          options.AddPolicy("ElevatedRights", policy =>
            policy.RequireRole("Approver"));
      });
      
    2. 然后这是问题发生的步骤。我在控制器中添加以下代码(请参阅步骤3)。我用[更改] [授权]方法 [Authorize(Policy = "ElevatedRights")] 但我被拒绝了。我甚至试过[Authorize(Roles = "Approver")]

    3. 我错过了什么或做错了什么?

      P.S。随意为问题提出更好的标题。

1 个答案:

答案 0 :(得分:1)

似乎它不知道将角色映射到哪个声明。

您可以改为使用<div class="container"> <div>Card</div> <div>Title</div> </div>

另一种可能的方法(应该有效)是在RequireClaim(ClaimTypes.Role, "Approver")来电中指定RoleClaimsType。类似的东西:

AddJwtBearer

您可以尝试AddJwtBearer(o => o.TokenValidationParameters = new TokenValidationParameters { RoleClaimType = ClaimTypes.Role })`.

,而不是ClaimTypes.Role
相关问题