控制器中授权角色的继承 类

Inheritance of Authorized Roles in controller classes

我创建了控制器 classes 来协助角色授权。

我有一个基地classControllersAuthorities,这是最高权限。我创建了其他 classes 来扩展每个基础 class.

[Authorize(Roles = "Owner")]
public abstract class ControllerAuthorities:Controller { }
[Authorize(Roles = "Admin")]
public abstract class AdminController:ControllerAuthorities { }

[Authorize(Roles = "Employee")]
public abstract class EmployeeController:AdminController { }
[Authorize(Roles = "Sales")]
public abstract class SalesController:EmployeeController { }

第一个问题,OwnerAdminEmployee 角色是否可以访问 SalesController

在我的项目控制器中实现这些 classes 时。 如果我不对 [Authorize] 进行注释,这会覆盖继承的授权角色吗?

//[Authorize]
public class AccountController:ControllerAuthorities
{

正在查看 Authorize attributeAttributeUsage 属性;

[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, 
    Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter

Inherited= true表示class的子class装饰有该属性的可以继承该属性。

AllowMultiple=true表示这个属性可以在同一个实体上放置多次。

具有继承的属性并允许使用相同的属性,您的 SalesController 可以被视为

[Authorize(Roles = "Sales")]
[Authorize(Roles = "Employee")]
[Authorize(Roles = "Admin")]
[Authorize(Roles = "Owner")]
public abstract class SalesController:EmployeeController { }

您可以在运行时使用这段代码进行测试。

var a = typeof(SalesController).GetCustomAttributes(true).ToArray();

第一个问题OwnerAdminEmployee 角色是否可以访问 SalesController? 继承的属性是分开的,因此它们被应用 independently.For 一个用户访问 SalesController ,用户必须具有所有角色(owner ,admin ,employeesales) 没有之一。

查看

之间的区别
[Authorize(Roles = "Sales")]
[Authorize(Roles = "Employee")]
[Authorize(Roles = "Admin")]
[Authorize(Roles = "Owner")]
public abstract class SalesController:EmployeeController { }

[Authorize(Roles = "Owner,Admin,Employee,Sales")]
public abstract class SalesController:EmployeeController { }

第二个问题:如果你离开 [Authorize] 未注释的相同逻辑 AccountController 就像

[Authorize(Roles = "Owner")]
[Authorize]
public class AccountController:ControllerAuthorities{}

所以它不会覆盖继承的权限,只是创建授权属性的多次使用,因为 Authorize 属性允许多次使用。如果 AllowMultipleAuthorize 属性定义中是 false,那么派生的 class 可以覆盖基础 class.

中的属性

will the Owner, Admin and Employee Roles have access to the SalesController?

不,他们无法访问 SalesController。继承使您的代码像这样:

[Authorize(Roles = "Owner")]
public abstract class ControllerAuthorities:Controller { }
[Authorize(Roles = "Admin", "Owner")]
public abstract class AdminController:Controller { }

[Authorize(Roles = "Employee", "Admin", "Owner")]
public abstract class EmployeeController:Controller { }
[Authorize(Roles = "Sales", "Employee", "Admin", "Owner")]
public abstract class SalesController:Controller { }

并且由于 SalesController 需要额外的角色,名为 Sales 的角色将无法访问。 访问密钥SalesController用户应该属于所有提到的角色。

If I leave the [Authorize] uncommented, will this override the inherited authority Role?

是的,因为 AccountController 派生自 ControllerAuthorities,需要 Owner 角色。

请注意, MVC 中的控制器只是 类 具有一些处理请求的附加功能。 class 概念没有区别。

提示:请看以下内容:

  • [Authorize(Roles = "Sales, Employee, Admin, Owner")] 允许 具有其中一个角色 的用户。换句话说,这行为 像 OR (||) 操作。
  • [Authorize(Roles = "Sales", "Employee", "Admin", "Owner")] 允许 拥有 所有角色 的用户。换句话说,这行为 像(&)运算。

最后一个跟你的问题一样。这也等于以下内容:

[Authorize(Roles = "Owner")]
[Authorize(Roles = "Admin")]
[Authorize(Roles = "Employee")]
[Authorize(Roles = "Sales")]

比这更清楚!见 How to authorize a set of controllers without placing the annotation on each one?