2

I have a custom authorization attribute, required only for some actions, which checks the request headers for a custom token. The token is checked in a database. Checking the database requires access to a service which I would like to have injected through the constructor.

The way I have read this can be done (here, here, and here) is by having a constructor-less filter and injecting the dependent one like this:

kernel.BindFilter<MyAuthorizeFilter>(FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();

However the BindFilter method is not available to me as I have setup Ninject as described here. This is using Ninject.Web.Common instead of Ninject MVC3 as I read that Ninject MVC3 would not work with MVC4 RC. How else can I go about accomplishing this?

I have read also that I could add to GlobalFilters.Filters - however I don't want it to be present on every action.

Thanks in advance for any answers.

Community
  • 1
  • 1
bean
  • 1,091
  • 1
  • 12
  • 23

1 Answers1

1

I'm not completely sure I see how you have set up your application, but my experience has been that if you want a filter for a WebApi controller you need to add it to the HttpFilterCollection that is available from the GlobalConfiguration.Filters. This is a Different set of filters than what MVC uses (through the GlobalFilterCollection).

Chris
  • 772
  • 3
  • 8
  • Hi Chris. I'm not sure I follow - in my Global.asax there is no property of GlobalConfiguration called Filters. I can Access GlobalFilters.Filters. Or GlobalConfigurationFilters though! However - I would've thought this does not help me in this scenario as I mentioned that I do not want to apply this filter to every action - only to some. – bean Sep 03 '12 at 10:33