title: Dependency Injection in attributes
body: <p>I am trying to inject a dependency into a custom <code>AuthorizeAttribute</code> as follows:</p>
  
  <pre><code>public class UserCanAccessArea : AuthorizeAttribute
  {
      readonly IPermissionService permissionService;
  
      public UserCanAccessArea() :
          this(DependencyResolver.Current.GetService&lt;IPermissionService&gt;()) { }
  
      public UserCanAccessArea(IPermissionService permissionService)
      {
          this.permissionService = permissionService;
      }
  
      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
          string AreaID =
              httpContext.Request.RequestContext.RouteData.Values["AreaID"] as string;
  
          bool isAuthorized = false;
  
          if (base.AuthorizeCore(httpContext))
              isAuthorized = permissionService.UserCanAccessArea(AreaID, httpContext.User);
  
          return isAuthorized;
      }
  }
  </code></pre>
  
  <p>This works but seems to be resolving as a singleton meaning I get the problems described in my <a href="https://stackoverflow.com/questions/29901631/prevent-authorizeattribute-from-caching">pervious question</a></p>
  
  <p>What I'd like to do is use property injection but as my Attribute itself is not resolved by Unity I'm unable to find a way to configure the container to intercept and resolve a property. I have tried the following:</p>
  
  <pre><code>public class UserCanAccessArea : AuthorizeAttribute
  {
      public IPermissionService permissionService { get; set; }
  
      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
          string AreaID =
              httpContext.Request.RequestContext.RouteData.Values["AreaID"] as string;
  
          bool isAuthorized = false;
  
          if (base.AuthorizeCore(httpContext))
              isAuthorized = permissionService.UserCanAccessArea(AreaID, httpContext.User);
  
          return isAuthorized;
      }
  }
  </code></pre>
  
  <p>Container:</p>
  
  <pre><code>container.RegisterType&lt;UserCanAccessArea&gt;(new InjectionProperty("permissionService"));
  </code></pre>
  
  <p>But the property is always null at runtime.</p>
  
  <p>Has anyone achieved this and if so do you have an example?</p>