创建验证策略:
services.AddAuthorization(opt =>
opt.AddPolicy("DeleteRolePolicy", policy => policy.RequireClaim("Delete Role")));
使用 Authorize 特性进行策略校验:
[Authorize(policy: "DeleteRolePolicy")]
[HttpPost]
public async Task<IActionResult> DeleteRole(string id)
{
var role = await _roleManager.FindByIdAsync(id);
...
}
重新登录后用户声明的权限才会起效。
单个策略中还可以添加多个声明:
services.AddAuthorization(opt =>
opt.AddPolicy("ManageRolePolicy",
policy => policy.RequireClaim("Delete Role").RequireClaim("Create Role")));