I’m trying to write an attribute class that can be used on all my “admin” actions which then effectively applies all relevant action/auth filters to that action. For instance, I always apply the AuthoriseAttribute and a custom AdminLayoutAttribute, so my intention is to just have an AdminAttribute which somehow implies both the above. That way I can make changes to the entire Admin section in one handy place.
However I don’t see how I can do this. Can any one point me in the right direction?
Much appreciated.
You could consider moving them from the actions to a controller class or a base controller class for the Admin actions. That way you would just set both the
AuthorizeandAdminLayoutattributes in the controller class instead of each individual action method.Having an attribute combining the authorization and some custom layout logic could be seen as a break of the Single Responsibility Principle, but if you have no other choice then you could try these approaches:
If you want to replace your existing
AdminLayoutAttributeby a newAdminAttribute, you could then create theAdminAttributeso it inherits from the standardAuthorizeAttributeand also implements IActionFilter\IResultFilter as in your custom AdminLayoutAttribute.Otherwise if you want to keep your existing
AdminLayoutAttributeand add a newAdminAttributethat combines it with theAuthorizeAttribute, you could then inherit from your custom attribute and implement IAuthorizationFilter by calling an internal instance of type AuthorizationFilterThe key in both options is to not having the new AdminAttribute class inheriting from 2 concrete classes, as multiple inheritance is not supported by C#.