Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action filter execution order

Tags:

I have created two classes that implement AuthorizeAttribute.

One is used globally, and I set it on the Global.asax.cs:

filters.Add(new FirstAuthorizeAttribute() { Order = 0 }); 

The other is called SecondAuthorizeAttribute and it is used only in some action methods, and I use it as attribute in the methods I want.

    [HttpGet]     [SecondAuthorize]     public ActionResult LogOut()     {         FormsAuthentication.SignOut();         Session.Clear();         Session.Abandon();         return Redirect(Url.Content("~/"));     } 

The problem is that SecondAuthorizeAttribute always execute before FirstAuthorizeAttribute, and I need this one to execute first. The order is not being helpful, how could I do it?

like image 796
vtortola Avatar asked Oct 25 '11 14:10

vtortola


People also ask

What is the order in which the action filters are executed?

Filters execute in this order: Authorization filters. Action filters. Response/Result filters.

Which action filter triggers first?

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. Authorization filters are used to implement authentication and authorization for controller actions.

Which filter will execute first in MVC?

as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.

What is the order of the filters that get executed if multiple filters are implemented?

Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.


2 Answers

The link in @HectorCorrea's answer is dead at the moment, here's the content retrieved and summarised from the current Google cache (in case that also goes) :

Filters execute in this order:

  • Authorization filters
  • Action filters
  • Response/Result filters
  • Exception filters

Within each filter, you may specify the Order property. (All filters are derived from the abstract class FilterAttribute, and this class has an Order property). This property will ensure the filter runs in a specific Order.

eg:

[AuthorizationFilterA(Order=2)] [AuthorizationFilterB(Order=1)] public ActionResult Index() {               return View(); } 

There's also FilterScope and, by default, the filter with the lowest scope runs first when the order is the same (or not specified):

namespace System.Web.Mvc {     public enum FilterScope {         First = 0,         Global = 10,         Controller = 20,         Action = 30,         Last = 100,     } } 

If no order is specified, the order value is -1 (first, not last).

Controllers themselves can be filters and will run with order Int32.MinValue

like image 66
freedomn-m Avatar answered Oct 19 '22 05:10

freedomn-m


This is a long shot, but have you tried using the Global and First values for your FirstAuthorizeAttribute ?

http://msdn.microsoft.com/en-us/library/system.web.mvc.filterscope(v=vs.98).aspx

http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx

like image 22
Hector Correa Avatar answered Oct 19 '22 03:10

Hector Correa