title: Configure Unity DI for ASP.NET Identity
body: <p>I'm using Unity successfully for all regular constructor injection such as repositories etc., but I can't get it working with the ASP.NET Identity classes. The setup is this:</p>
  
  <pre><code>public class AccountController : ApiController
  {
      private UserManager&lt;ApplicationUser&gt; _userManager { get; set; }
  
      public AccountController(UserManager&lt;ApplicationUser&gt; userManager)
      {
          if (userManager == null) { throw new ArgumentNullException("userManager"); }
          _userManager = userManager;
      }
  
      // ...
  }
  </code></pre>
  
  <p>with these configs for Unity:</p>
  
  <pre><code>unity.RegisterType&lt;AccountController&gt;();
  unity.RegisterType&lt;UserManager&lt;ApplicationUser&gt;&gt;(new HierarchicalLifetimeManager());
  unity.RegisterType&lt;IUserStore&lt;ApplicationUser&gt;, UserStore&lt;ApplicationUser&gt;&gt;(new HierarchicalLifetimeManager());
  </code></pre>
  
  <p>That's the same as other posts here for Autofac, Ninject e.g., but it doesn't work in my case. The error message is:</p>
  
  <blockquote>
    <p>An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.
    Manual creation works of course:</p>
  </blockquote>
  
  <pre><code>public AccountController()
      : this(new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;("Mongo")))
  {
  }
  </code></pre>
  
  <p>What's wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As suggested by @emodendroket, shortening the code to this does the trick. No need for the Unity.Mvc package.</p>
  
  <pre><code>unity.RegisterType&lt;IUserStore&lt;IdentityUser&gt;, 
    MyCustomUserStore&gt;(new HierarchicalLifetimeManager());
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public AccountController(UserManager&lt;IdentityUser&gt; _userManager,
    IAccountRepository _account)
  {
      // ...
  </code></pre>