I would like to add authorization to the controller for several roles at once.
Usually it will look like this:
[Authorize(Roles = "RoleA,RoleB,RoleC")] public async Task<ActionResult> Index() { }
But I kept my roles in const, as they can be changed or expanded at some point.
public const RoleA = "RoleA"; public const RoleB = "RoleB"; public const RoleC = "RoleC";
I cannot do this, since the string must be known at compile time:
[Authorize(Roles = string.join(",",RoleA,RoleB,RoleC)] public async Task<ActionResult> Index() { }
Is there a way around the problem?
I CAN write a const that simply contains "RoleA, RoleB, RoleC", but I don't like magic lines, and this is a magic line. Changing the role name and forgetting to change the combo string would be a disaster.
I am using MVC5. The ASP.NET identifier and role are known at compile time.
c # authorization asp.net-mvc
Christian Sauer Jun 12 '14 at 10:14 2014-06-12 10:14
source share