ASP.Core 中基于角色的授权 3:如何授予特定角色无处不在的访问权限?
Role based authorization in ASP.Core 3: how to grant access everywhere to certain role?
我正在编写一个 ABAC 系统,我将在其中根据某些 roles/atributes/etc 来决定用户是否可以访问某些数据。然而,有一种特殊的用户(类似于超级管理员)应该能够随时随地访问所有内容。我不想遍历所有策略、控制器、操作和方法并添加对这个特定角色的检查。有没有办法以更集中的方式做到这一点? (例如:在startup
)。
如果无法将它添加到全局位置,我正在考虑至少在控制器级别全局添加它:我正在寻找 here 并且我看到装饰器 [Authorize(Roles = "Administrator")]
允许您将对特定 method/class 的访问权限限制为管理员用户。但是,我想要 "the opposite"。我的意思是类似 AuthorizeAlways
的东西具有以下行为:
[AuthorizeAlways(Roles = "SuperAdministrator")]
public class ControlPanelController : Controller
{
[Authorize(Roles = "SetterUser")]
public ActionResult SetTime()
{
}
[Authorize(Roles = "PowerUser")]
[MinimumAgeAuthorize(50)]
public ActionResult ShutDown()
{
}
}
在这种情况下,我希望 SuperAdministrator
(即使他们已经 49 岁)可以访问任何地方。 SetterUser
只能访问 SetTime
,只有 50 岁以上的 PowerUser
才能访问 ShutDown
。
我不知道这是否有意义。可能吗?我在哪里可以做?谢谢!
这篇博客 post 提供了一个很好的教程来说明如何实现自定义授权:
https://seanspaniel.wordpress.com/2019/12/13/custom-authorization-in-asp-net-core-3/
根据该教程,在 CustomAuthorizationMiddleware class 中,您可以检查 "SuperAdministrator" 角色并授予对每个端点的访问权限。
public static class CustomAuthorizationMiddleware
{
public static async Task Authorize(HttpContext httpContext, Func next)
{
var endpointMetaData = httpContext.GetEndpoint().Metadata;
bool hasCustomAuthorizeAttribute = endpointMetaData.Any(x => x is CustomAuthorizeAttribute);
if (!hasCustomAuthorizeAttribute)
{
await next.Invoke();
return;
}
CustomAuthorizeAttribute customAuthorizeAttribute = endpointMetaData
.FirstOrDefault(x => x is CustomAuthorizeAttribute) as CustomAuthorizeAttribute;
// Check if user has allowed role or super administrator role
bool isAuthorized = customAuthorizeAttribute.AllowedUserRoles
.Any(allowedRole => httpContext.User.IsInRole(allowedRole))
|| httpContext.User.IsInRole("SuperAdministrator");
if (isAuthorized)
{
await next.Invoke();
return;
}
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await httpContext.Response.WriteAsync("unauthorized");
}
}
我正在编写一个 ABAC 系统,我将在其中根据某些 roles/atributes/etc 来决定用户是否可以访问某些数据。然而,有一种特殊的用户(类似于超级管理员)应该能够随时随地访问所有内容。我不想遍历所有策略、控制器、操作和方法并添加对这个特定角色的检查。有没有办法以更集中的方式做到这一点? (例如:在startup
)。
如果无法将它添加到全局位置,我正在考虑至少在控制器级别全局添加它:我正在寻找 here 并且我看到装饰器 [Authorize(Roles = "Administrator")]
允许您将对特定 method/class 的访问权限限制为管理员用户。但是,我想要 "the opposite"。我的意思是类似 AuthorizeAlways
的东西具有以下行为:
[AuthorizeAlways(Roles = "SuperAdministrator")]
public class ControlPanelController : Controller
{
[Authorize(Roles = "SetterUser")]
public ActionResult SetTime()
{
}
[Authorize(Roles = "PowerUser")]
[MinimumAgeAuthorize(50)]
public ActionResult ShutDown()
{
}
}
在这种情况下,我希望 SuperAdministrator
(即使他们已经 49 岁)可以访问任何地方。 SetterUser
只能访问 SetTime
,只有 50 岁以上的 PowerUser
才能访问 ShutDown
。
我不知道这是否有意义。可能吗?我在哪里可以做?谢谢!
这篇博客 post 提供了一个很好的教程来说明如何实现自定义授权: https://seanspaniel.wordpress.com/2019/12/13/custom-authorization-in-asp-net-core-3/
根据该教程,在 CustomAuthorizationMiddleware class 中,您可以检查 "SuperAdministrator" 角色并授予对每个端点的访问权限。
public static class CustomAuthorizationMiddleware
{
public static async Task Authorize(HttpContext httpContext, Func next)
{
var endpointMetaData = httpContext.GetEndpoint().Metadata;
bool hasCustomAuthorizeAttribute = endpointMetaData.Any(x => x is CustomAuthorizeAttribute);
if (!hasCustomAuthorizeAttribute)
{
await next.Invoke();
return;
}
CustomAuthorizeAttribute customAuthorizeAttribute = endpointMetaData
.FirstOrDefault(x => x is CustomAuthorizeAttribute) as CustomAuthorizeAttribute;
// Check if user has allowed role or super administrator role
bool isAuthorized = customAuthorizeAttribute.AllowedUserRoles
.Any(allowedRole => httpContext.User.IsInRole(allowedRole))
|| httpContext.User.IsInRole("SuperAdministrator");
if (isAuthorized)
{
await next.Invoke();
return;
}
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await httpContext.Response.WriteAsync("unauthorized");
}
}