Search code examples
asp.net-mvcasp.net-mvc-3controllerauthorizeauthorize-attribute

How to authorize a set of controllers without placing the annotation on each one?


I have sets of controllers which are each used for each authorization type. For example, a class A authorization will have a set of controllers each which require class A authorization. Is there a way to place one [Authorize(Role="Class A")] attribute somewhere which will apply to each of those controllers without having to decorate each controller with the same attribute?


Solution

  • You can initialize those controllers derived from your base controller. namely put your attribute on a controller base class and to ensure that each controller within derived from base class.

    [Authorize(Role="Class A")]
    public class CustomBaseController : Controller{}
    
    public class AController: CustomBaseController{}
    
    public class BController: CustomBaseController{}