asp.net-mvcasp.net-mvc-filters

ASP.NET MVC IAuthorizationFilter usage


In ASP.NET MVC, IAuthorizationFilter will run before any other filters and action methods. So is it appropriate to implement IAuthorizationFilter for some scenarios that some logic of checking and analysing the incoming HttpRequest needs to be executed before any other logic runs? Or IAuthorizationFilter should be used only for Authorization related logic, then what other ways should I take for this?


Solution

  • Its not good idea to use IAuthorizationFilter for any thing other than Authorization. You can just create a global filter with order so your filter can execute before any other actionfilters

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new SpecialFilterAttribute(), 1);
        filters.Add(new LogFilter(), 2);
    }
    

    Here is the other SO question related to it