Authorize Attribute with Multiple Roles

前端 未结 5 1291
长发绾君心
长发绾君心 2020-12-02 07:00

I would like to add Authorization to a controller, for multiple Roles at once.

Normally that would look like this:

[Authorize(Roles = \"RoleA,RoleB,R         


        
5条回答
  •  攒了一身酷
    2020-12-02 07:04

    The best and simplest way I found to resolve this problem is just to concatenate roles in the Authorize attribute.

    [Authorize(Roles = CustomRoles.Admin + "," + CustomRoles.OtherRole)]
    

    with CustomRole a class with constant strings like this :

    public static class CustomRoles
    {
        public const string Admin = "Admin";
        // and so on..
    }
    

提交回复
热议问题