title: Dependency injecting UserStore in OWIN startup using Ninject OWIN middleware
body: <p>I am having problems creating a custom UserStore using dependency injection when creating an ApplicationUserManager using the OWIN request pipeline.</p>
  
  <p><strong>Background</strong></p>
  
  <p>I am trying to migrate the user functionality in our web application from using the SimpleMembership to the new ASP.NET Identity. When starting a new MVC 5 project, the default implementation of the single page application uses ASP.Identity, using Entity Framework to implement the UserStore functionality.</p>
  
  <p>In my case, we are already using NHibernate as the ORM, and using ninject to implement the unit of work pattern so that we had one NHibernate session per request, and I wanted to make the ASP.Identity work with our existing framework.</p>
  
  <p>To this end, I created a custom UserStore, which could be created by injecting the relevant repositories/nhibernate session, etc. This could then be injected into the Controller's constructor using Ninject, rather than using the default implementation's GetOwinContext functionality. </p>
  
  <p>In order to do this, I had commented out the following line in the ConfigureAuth(IAppBuilder app) method of the Startup, which by default creates the UserManager class:</p>
  
  <pre><code>// app.CreatePerOwinContext&lt;ApplicationUserManager&gt;(ApplicationUserManager.Create);
  </code></pre>
  
  <p>Instead, I used the NinjectWebCommon created when installing the Ninject.Web.Common.Webhost nuget package to create the relevant bindings.</p>
  
  <p>This implementation worked fine with some of the UserManager operations, but with some operations, such as ResetPasswordAsync, it fails because the default ApplicationUserManager implementation is not called, and so the UserTokenProvider in the UserManager class is never set:</p>
  
  <pre><code>    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,
          };
          // 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 = "Security Code",
              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;
      }
  </code></pre>
  
  <p>Therefore, the UserTokenProvider is not set.</p>
  
  <p>Problem</p>
  
  <p>I want to use the OWIN pipeline, because Visual Studio's default implementation of the ApplicationUserManager class injects the IDataProtectionProvider in its Create callback method. However, I also want to create my UserStore using dependency Injection, and I do not know how to create a UserStore within this method using dependency injection.</p>
  
  <pre><code>    public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context)
      {
          // WANT TO CREATE THE USER STORE USING NINJECT DEPENDENCY INJECTION HERE
          // var userStore = ...
          var manager = new ApplicationUserManager(userStore);
      }
  </code></pre>
  
  <p>I have tried to get around this limitation by using the Ninject.Web.Common.OwinHost nuget package and creating the kernel within the Startup class.</p>
  
  <pre><code>    public void ConfigureAuth(IAppBuilder app)
      {
          // Setup
  
          app.UseNinjectMiddleware(CreateKernel);
      }
  </code></pre>
  
  <p>However, the Ninject.Web.Common.OwinHost does not expose its Kernel, so I am unable to use service location pattern to inject the values into my custom UserStore in the Create callback.</p>
  
  <p>I have also tried to create a singleton Kernel, and register this using app.CreatePerOwinContext(CreateKernel) with the relevant delegate, so I could later access the Kernel, but when I call context.Get() it just returns null.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I register a callback function with CreatePerOwinContext to create a custom UserManager which uses a custom UserStore, and then use Ninject to create the custom UserStore using dependency injection in the Create callback, so that I also have access to the IdentityFactoryOptions which Owin uses to inject the user token provider? </p>