Authorization filter attribute is not firing
public class AuthenticationRequiredAttribute : System.Web.Http.Filters.AuthorizationFilterAttribute
{
public override bool AllowMultiple
{
get { return false; }
}
public override void OnAuthorization(HttpActionContext actionContext)
{
base.OnAuthorization(actionContext);
}
I have decorated controller with the attribute
[AuthenticationRequired]
public class ProfileController : BaseController
{
public ProfileController(IMyRepository repository)
: base(repository)
{
}
}
The AuthenticationRequired filter is not firing, what am I missing?
You seem to be using a Web API filter instead of an MVC filter. Inherit your custom authorization filter from
System.Web.Mvc.FilterAttribute
class andSystem.Web.Mvc.IAuthorizationFilter
interface, and implementOnAuthorization()
method.For example,
You can configure your custom filter in your application at three levels:
Global level by registering your filter in
Application_Start
event ofGlobal.asax.cs
;Controller level by decorating a controller with your filter by putting your filter at the top of the controller name;
Action level by decorating a given action method with your filter in a similar way as above for controllers.
In your case you simply need to decorate your controller with your custom filter.
Hope this helps.