Azure Active Directory 用户角色和授权
Azure Active Directory user roles and authorization
我在 Azure 上托管了应用程序,Angular 4 在前面,.net core 2.0 在 back-end 上。
我想要实现的是:为添加到我的 Azure Active Directory 的用户添加角色。
身份验证已实施并且运行良好。我使用 ADAL 并随每个请求发送不记名令牌。
这些是我在 Azure 门户清单中定义的应用程序角色:
"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"
}
]
批准者角色已分配给所需用户。
我向 back-end (.net core 2.0 web api) 发送请求,其中我有 [Authorize] 属性并检查用户声明 User.Claims.ToList() 然后我收到用户批准人:
[15]{http://schemas.microsoft.com/ws/2008/06/identity/claims/role:
Approver}
太棒了!
现在我在startup.cs
中添加策略
services.AddAuthorization(options =>
{
options.AddPolicy("ElevatedRights", policy =>
policy.RequireRole("Approver"));
});
那么就是这一步出现问题了。我在控制器中添加以下代码(参见步骤 3)。我更改了 [Authorize] 方法
[Authorize(Policy = "ElevatedRights")]
但我当时被拒绝了。我什至尝试过 [Authorize(Roles = "Approver")]
我错过了什么或做错了什么?
P.S。欢迎提出更好的问题标题。
似乎它不知道将角色映射到哪个声明。
您或许可以改用 RequireClaim(ClaimTypes.Role, "Approver")
。
另一种可能的方法(应该有效)是在您的 AddJwtBearer
调用中指定 RoleClaimsType
。类似于:
AddJwtBearer(o => o.TokenValidationParameters = new TokenValidationParameters { RoleClaimType = ClaimTypes.Role })`.
代替ClaimTypes.Role
,您也可以尝试"roles"
。
我在 Azure 上托管了应用程序,Angular 4 在前面,.net core 2.0 在 back-end 上。 我想要实现的是:为添加到我的 Azure Active Directory 的用户添加角色。 身份验证已实施并且运行良好。我使用 ADAL 并随每个请求发送不记名令牌。
这些是我在 Azure 门户清单中定义的应用程序角色:
"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" }
]
批准者角色已分配给所需用户。
我向 back-end (.net core 2.0 web api) 发送请求,其中我有 [Authorize] 属性并检查用户声明 User.Claims.ToList() 然后我收到用户批准人:
[15]{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Approver}
太棒了!
现在我在startup.cs
中添加策略services.AddAuthorization(options => { options.AddPolicy("ElevatedRights", policy => policy.RequireRole("Approver")); });
那么就是这一步出现问题了。我在控制器中添加以下代码(参见步骤 3)。我更改了 [Authorize] 方法
[Authorize(Policy = "ElevatedRights")]
但我当时被拒绝了。我什至尝试过[Authorize(Roles = "Approver")]
我错过了什么或做错了什么?
P.S。欢迎提出更好的问题标题。
似乎它不知道将角色映射到哪个声明。
您或许可以改用 RequireClaim(ClaimTypes.Role, "Approver")
。
另一种可能的方法(应该有效)是在您的 AddJwtBearer
调用中指定 RoleClaimsType
。类似于:
AddJwtBearer(o => o.TokenValidationParameters = new TokenValidationParameters { RoleClaimType = ClaimTypes.Role })`.
代替ClaimTypes.Role
,您也可以尝试"roles"
。