title: Custom Authorization MVC 3 and Ninject IoC
body: <p>I have a custom authorization class that inherits from FilterAttribute and implements IAuthorizationFilter. I am using the latest version of Ninject w/ asp.net MVC 3 support. </p>
  
  <p>The problem I have is I am using constructor injection to inject a repository. But by the the time OnAuthorization is called, the repository is null. Here is the code...</p>
  
  <pre><code>public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
      {
          private readonly IMyRepo _MyRepo;
  
          public MyAuthorizeAttribute() { }
          public MyAuthorizeAttribute(IMyRepo myRepo)
          {
              _MyRepo= myRepo; //this gets initialized
          }
  
  
          public void OnAuthorization(AuthorizationContext filterContext)
          {
              _MyRepo.DoStuff(); //&lt;&lt; Null, wtf
  
          }
      }
  </code></pre>
  
  <p>Filter Binding:</p>
  
  <pre><code>Bind&lt;IMyRepo&gt;().To&lt;MyRepo&gt;().InRequestScope();
  
  
  this.BindFilter&lt;MyAuthorizeAttribute &gt;(System.Web.Mvc.FilterScope.Controller, null).WhenControllerHas&lt;MyAuthorizeAttribute &gt;();
  </code></pre>
  
  <p><strong>Update:</strong>
  One thing I noticed is this filter is at controller level. I have other filters at action scope that seem to work properly...could that be the reason?</p>
  
  <p><strong>Update 2:</strong>
  I've confirmed that if I change the filter scope to action, then the repository is available OnAuthorization (not null).</p>
  
  <p>This works below, however I need at controller scope, not action.</p>
  
  <pre><code>this.BindFilter&lt;MyAuthorizeAttribute &gt;(System.Web.Mvc.FilterScope.Action, null).WhenActionMethodHas&lt;MyAuthorizeAttribute &gt;();
  </code></pre>