I have implemented my own custom Authorization Attribute in MVC 4 by inheriting from AuthorizeAttribute class. I also have a custom ActionFilterAttribute. These both work fine however the problem lies in ordering them. I need the custom Action Filter to run before the custom Authorize Filter.
I have tried using the Order property for the attributes but as i understand it, Authorize Filters will always run before Action Filters.
Does anyone know how to force the Action Filter to execute before the Authorize Filter??
When you take a look at the source code (available at http://aspnetwebstack.codeplex.com/) you see that this is not possible with the standard Filter classes.
IAuthorizationFilterimplementations are always executed beforeIActionFilterimplementations. That’s because action filters will not run when authorization filters return a result.To solve this you can create your own
ControllerActionInvokerdescendant class and override theInvokeActionmethod:You need to inject your custom
MyControllerActionInvokerclass into your controllers in a customControllerFactoryclass:And of course you now have to register your own
MyControllerFactorywith the MVC framework. You should do this where you’re also registering your routes:This implementation works fine here.