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.

2
  • Having same issue, did you ever resolve this? Commented Sep 28, 2012 at 20:33
  • Sorry - I did not solve it! In the constructor for my auth attribute I'm providing an instantiation to the repository. Commented Oct 2, 2012 at 16:11

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.