title: Actionfilter Injection in ASP.NET MVC 5
body: <p>I have a simple filter.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class IsAdmin : ActionFilterAttribute, IAuthenticationFilter
  {
      private string _roleName;
      IBusinessIdentity _identity;
  
      public IsAdmin(string roleName, IBusinessIdentity identity)
      {
          this._roleName = roleName;
          this._identity = identity;
      }
  
      public void OnAuthentication(AuthenticationContext filterContext)
      {
      }
  
      public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
      {
          if (!_identity.Roles.Contains(_roleName))
              filterContext.Result = new HttpUnauthorizedResult();
      }
  }
  </code></pre>
  
  <p>I am using Ninject. Here is my controller. I'm trying to get the injected service into my ActionFilter in order not to take a dependency on <code>HttpContext</code> but instead on my <code>IBusinessIdentity</code>.</p>
  
  <p><code>IBusinessIdentity</code> gets injected the HttpContext.User.Identity`. And it does a few database calls and gets the userRoles.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class HomeController : Controller
  {
      readonly IBusinessIdentity _identity;
  
      public HomeController(IBusinessIdentity identity)
      {
          this._identity= identity;
      }
  
      [IsAdmin("Admin", _identity)]
      public ActionResult Index()
      {
          return View();  
      }
  }
  </code></pre>
  
  <p>This does not work and I'm getting a compiler error when I try to put the "identity" in the actionfilter constructor at compile time.</p>
  
  <blockquote>
    <p>An object reference is required for the non-static field, method, or property</p>
  </blockquote>
  
  <p>I need this because I'm planning to test various permissions with the identity.</p>
  
  <p>I am thinking some kind of reflection to be done after controllers gets instantiated. I have a very vague idea on how it could be done.</p>
  
  <p>I am using ASP.NET MVC 5 and I don't have the kernel.bindfilter. I can't use older version.</p>
  
  <p>I am well aware of this hack.</p>
  
  <p><a href="https://stackoverflow.com/questions/11333575/action-filter-constructor-being-called-repeatedly-for-single-controller">Action filter constructor being called repeatedly for single controller</a></p>
  
  <p><a href="https://github.com/ninject/Ninject.Web.Mvc/wiki/Conditional-bindings-for-filters" rel="nofollow noreferrer">https://github.com/ninject/Ninject.Web.Mvc/wiki/Conditional-bindings-for-filters</a></p>
  
  <p>How can I accomplish the same maybe effect using Ninject for MVC 5.</p>
  
  <p><b>EDIT</b>: massive failure</p>
  
  <p>I forgot to include the:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using Ninject.Web.Mvc.FilterBindingSyntax;
  </code></pre>
  
  <p>Now everything works as explained in the above links.</p>
  
  <p>Now I need to figure out how to inject the "roleName" string in the filter constructor. Although I think just construct a filter for every role. I will post entire code later.</p>