title: Microsoft Unity DI with Identity
body: <p>I've trawled through all the questions on SO but can't seem to work out what i'm doing wrong here.</p>
  
  <p>I have the following UserService</p>
  
  <pre><code>   public class UserService : BaseService
      {
          private readonly Func&lt;UserManager&lt;ApplicationUser&gt;&gt; _userManagerFactory;
          private readonly Func&lt;RoleManager&lt;IdentityRole&gt;&gt; _roleManagerFactory;
  
          public UserService(Func&lt;UserManager&lt;ApplicationUser&gt;&gt; userManagerFactory, Func&lt;RoleManager&lt;IdentityRole&gt;&gt; roleManagerFactory)
          {
              _userManagerFactory = userManagerFactory;
              _roleManagerFactory = roleManagerFactory;
          }
  </code></pre>
  
  <p>and in my accounts controller i inject as follows;</p>
  
  <pre><code>public class AccountsController : ApiController
  {
      [Dependency]
      public UserService UserService { get; set; }
  </code></pre>
  
  <p>Now in my UnityConfig.cs I set the DI as follows;</p>
  
  <pre><code>var userInjectionConstructor = new InjectionConstructor(new MyDataContext());
              //container.RegisterType&lt;IUserStore&lt;ApplicationUser&gt;, UserStore&lt;ApplicationUser&gt;&gt;(userInjectionConstructor);
              container.RegisterType&lt;UserManager&lt;ApplicationUser&gt;, ApplicationUserManager&gt;();
              container.RegisterType&lt;IRoleStore&lt;IdentityRole, string&gt;, RoleStore&lt;IdentityRole&gt;&gt;(userInjectionConstructor);
  </code></pre>
  
  <p>Where the ApplicationUserManager is as follows;</p>
  
  <pre><code>public class ApplicationUserManager : UserManager&lt;ApplicationUser&gt;
  {
      public ApplicationUserManager(IUserStore&lt;ApplicationUser&gt; store)
          : base(store)
      {
      }
  
      public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context)
      {
          var container = (IUnityContainer)Startup.HttpConfiguration.DependencyResolver.GetService(typeof(IUnityContainer));
          var manager = new ApplicationUserManager(new UserStore&lt;ApplicationUser&gt;(new MyDataContext()));
          // Configure validation logic for usernames
          manager.UserValidator = new UserValidator&lt;ApplicationUser&gt;(manager)
          {
              AllowOnlyAlphanumericUserNames = false,
              RequireUniqueEmail = true
          };
          // Configure validation logic for passwords
          manager.PasswordValidator = new PasswordValidator
          {
              RequiredLength = 6,
              RequireNonLetterOrDigit = true,
              RequireDigit = true,
              RequireLowercase = true,
              RequireUppercase = true,
          };
          var dataProtectionProvider = options.DataProtectionProvider;
          if (dataProtectionProvider != null)
          {
              manager.UserTokenProvider = new DataProtectorTokenProvider&lt;ApplicationUser&gt;(dataProtectionProvider.Create("ASP.NET Identity"));
          }
          return manager;
      }
  }
  </code></pre>
  
  <p>This compiles and runs, however, i get the error when it tries to resolve the DI;</p>
  
  <blockquote>
    <p>Resolution of the dependency failed, type = "Microsoft.AspNet.Identity.UserManager<code>1[CCS.Core.ApplicationUser]", name = "(none)".
    Exception occurred while: while resolving.
    Exception is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore</code>1[CCS.Core.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?</p>
  </blockquote>
  
  <p>Now this issue was raised because if I use the following;</p>
  
  <pre><code>var userInjectionConstructor = new InjectionConstructor(new MyDataContext());
  container.RegisterType&lt;IUserStore&lt;ApplicationUser&gt;, UserStore&lt;ApplicationUser&gt;&gt;(userInjectionConstructor);
  </code></pre>
  
  <p>Then the DI works, however, none of the ApplicationUserManager setup is there (token providers etc). </p>
  
  <p>Now the ApplicationUserManager.Create call is Static so I assume I'm going to have to move this into the constructor for the DI? </p>
  
  <p>Can anyone explain what I'm dong wrong here and how to resolve?</p>