title: Register IAuthenticationManager with Unity
body: <p>I'm using Unity for Dependencies Injection and using Identiy Provider to manage the user login, register, email confirmation, etc.</p>
  
  <p>When I try to register a user, I have this problem:</p>
  
  <blockquote>
    <p>The current type, Microsoft.Owin.Security.IAuthenticationManager, is
    an interface and cannot be constructed. Are you missing a type
    mapping?</p>
  </blockquote>
  
  <p>I have no idea how to register this Interface (IAuthenticationManager) in my Unity container.</p>
  
  <p>I tried registering the interface with this code, but if I put it, I have other problem:</p>
  
  <blockquote>
    <p>No IUserTokenProvider is registered.</p>
  </blockquote>
  
  <pre><code> container.RegisterType&lt;HttpContextBase&gt;(
              new InjectionFactory(_ =&gt; new HttpContextWrapper(HttpContext.Current)));
          container.RegisterType&lt;IOwinContext&gt;(new InjectionFactory(c =&gt; c.Resolve&lt;HttpContextBase&gt;().GetOwinContext()));
          container.RegisterType&lt;IAuthenticationManager&gt;(
              new InjectionFactory(c =&gt; c.Resolve&lt;IOwinContext&gt;().Authentication));
  </code></pre>
  
  <p>I put some code of the app (If I don't use Unity, all work fine):</p>
  
  <p><strong>AccountController</strong></p>
  
  <pre><code>private IAuthenticationManager AuthenticationManager
          {
              get
              {
                  return HttpContext.GetOwinContext().Authentication;
              }
          }
  </code></pre>
  
  <p><strong>IdentityConfig.cs</strong></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 manager = new ApplicationUserManager(new UserStore&lt;ApplicationUser&gt;(context.Get&lt;ApplicationDbContext&gt;()));
              // 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,
              };
              // Configure user lockout defaults
              manager.UserLockoutEnabledByDefault = true;
              manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
              manager.MaxFailedAccessAttemptsBeforeLockout = 5;
              // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
              // You can write your own provider and plug in here.
              manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider&lt;ApplicationUser&gt;
              {
                  MessageFormat = "Your security code is: {0}"
              });
              manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider&lt;ApplicationUser&gt;
              {
                  Subject = "SecurityCode",
                  BodyFormat = "Your security code is {0}"
              });
              manager.EmailService = new EmailService();
              manager.SmsService = new SmsService();
              var dataProtectionProvider = options.DataProtectionProvider;
              if (dataProtectionProvider != null)
              {
                  manager.UserTokenProvider =
                      new DataProtectorTokenProvider&lt;ApplicationUser&gt;(dataProtectionProvider.Create("ASP.NET Identity"));
              }
              return manager;
          }
      }
  
      // Configure the RoleManager used in the application. RoleManager is defined in the ASP.NET Identity core assembly
      public class ApplicationRoleManager : RoleManager&lt;IdentityRole&gt;
      {
          public ApplicationRoleManager(IRoleStore&lt;IdentityRole, string&gt; roleStore)
              : base(roleStore)
          {
          }
  
          public static ApplicationRoleManager Create(IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
          {
              return new ApplicationRoleManager(new RoleStore&lt;IdentityRole&gt;(context.Get&lt;ApplicationDbContext&gt;()));
          }
      }
  
      public class EmailService : IIdentityMessageService
      {
          public Task SendAsync(IdentityMessage message)
          {
              // Plug in your email service here to send an email.
              return Task.FromResult(0);
          }
      }
  
      public class SmsService : IIdentityMessageService
      {
          public Task SendAsync(IdentityMessage message)
          {
              // Plug in your sms service here to send a text message.
              return Task.FromResult(0);
          }
      }
  
      // This is useful if you do not want to tear down the database each time you run the application.
      // public class ApplicationDbInitializer : DropCreateDatabaseAlways&lt;ApplicationDbContext&gt;
      // This example shows you how to create a new database if the Model changes
      public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges&lt;ApplicationDbContext&gt;
      {
          protected override void Seed(ApplicationDbContext context)
          {
              InitializeIdentityForEF(context);
              base.Seed(context);
          }
  
          //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
          public static void InitializeIdentityForEF(ApplicationDbContext db)
          {
              var userManager = HttpContext.Current.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
              var roleManager = HttpContext.Current.GetOwinContext().Get&lt;ApplicationRoleManager&gt;();
              const string name = "admin@example.com";
              const string password = "Admin@123456";
              const string roleName = "Admin";
  
              //Create Role Admin if it does not exist
              var role = roleManager.FindByName(roleName);
              if (role == null)
              {
                  role = new IdentityRole(roleName);
                  var roleresult = roleManager.Create(role);
              }
  
              var user = userManager.FindByName(name);
              if (user == null)
              {
                  user = new ApplicationUser { UserName = name, Email = name };
                  var result = userManager.Create(user, password);
                  result = userManager.SetLockoutEnabled(user.Id, false);
              }
  
              // Add user admin to Role Admin if not already added
              var rolesForUser = userManager.GetRoles(user.Id);
              if (!rolesForUser.Contains(role.Name))
              {
                  var result = userManager.AddToRole(user.Id, role.Name);
              }
          }
      }
  
      public class ApplicationSignInManager : SignInManager&lt;ApplicationUser, string&gt;
      {
          public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
              :
                  base(userManager, authenticationManager)
          {
  
  
  
          }
  
          public override Task&lt;ClaimsIdentity&gt; CreateUserIdentityAsync(ApplicationUser user)
          {
              return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
          }
  
          public static ApplicationSignInManager Create(IdentityFactoryOptions&lt;ApplicationSignInManager&gt; options, IOwinContext context)
          {
              return new ApplicationSignInManager(context.GetUserManager&lt;ApplicationUserManager&gt;(), context.Authentication);
          }
      }
  </code></pre>
  
  <p>Thanks!!</p>