R – IAuthorizationFilter + Ninject2

asp.net-mvcauthorizationcustom-action-filterdependency-injectionninject

I'm currently using Ninject2 to bind the various services and repositories in my MVC app. That part seems to be working just fine. Now I'd like to also bind my own class to IAuthorizationFilter and all actions that have the attribute set.

I've created a class that inherits from AuthorizationFilter and Implements IAuthorizationFilter.

I've also add this to my binding module:

Bind(Of IAuthorizationFilter).To(Of MyAuthFilter)

The last time I checked, the Ninject Mvc bits had support for also binding the 4 types of action filters.

Has anyone else done this? Whenever I run the site, the url that invokes the action marked Authorize just redirect to the login page, and never hits the breakpoint in my filter class.

If I were using a custom attribute, it would work, but changing all of the Authorize attributes defeats the purpose of using Ninject every time I want to swap one out of course.

Best Answer

It's not enough to register MVC filters with Ninject; you also have to tell MVC when to execute them. That's why you still need the custom attribute. The injection support in Ninject.Web.Mvc is to allow dependencies to be injected into filters.

Related Topic