Action filters are just attributes. You have no control over when these attributes are created by the CLR. One option is to write a marker attribute:
public class CustomAuthorizationAttribute : Attribute { }
and then the actual action filter:
public class CustomAuthorizationFilter : ActionFilterAttribute { private readonly IAccountBL accountBL; public CustomAuthorizationFilter(IAccountBL accountBL) { this.accountBL = accountBL; } public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<CustomAuthorizationAttribute>().Any() || actionContext.ActionDescriptor.GetCustomAttributes<CustomAuthorizationAttribute>().Any()) {
and finally register it as a global action filter:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { ... IAccountBL accountBL = ... config.Filters.Add(new CustomAuthorizationFilter(accountBL)); } }
and finally you can use the marker attribute:
[CustomAuthorization] public class MemberController : ApiController { ... }
source share