Dependency injecting UserStore in OWIN startup using Ninject OWIN middleware Dependency injecting UserStore in OWIN startup using Ninject OWIN middleware asp.net asp.net

Dependency injecting UserStore in OWIN startup using Ninject OWIN middleware


For info:

It is possible to register the kernel as a singleton so that the same kernel can be used by the ninject middleware and also registered within the owin context.

    public static StandardKernel CreateKernel()    {        if (_kernel == null)        {            _kernel = new StandardKernel();            _kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();            _kernel.Load(Assembly.GetExecutingAssembly(), Assembly.Load("Super.CompositionRoot"));        }        return _kernel;    }

The callback function app.CreatePerOwinContext(ApplicationUserManager.Create), will call the ApplicationUserManager.Create rather than register it to be called at a later point during the setup. Therefore, the CreateKernel function needs to be registered before the ApplicationUserManager's Create callback or you will get a null reference exception if you try to get the kernel from the owin context within that method.

    public void ConfigureAuth(IAppBuilder app)    {        app.CreatePerOwinContext(CreateKernel);        app.UseNinjectMiddleware(CreateKernel);        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);     }

This will allow you to access the kernel to create a custom UserStore within the ApplicationUserManager's Create callback:

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)    {        var kernel = context.Get<StandardKernel>();        var userStore = kernel.Get<IUserStore<User, int>>();        var manager = new ApplicationUserManager(userStore);        //...    }

I know that in general dependency injection should be favoured over service location, but in this context I couldn't see a way around it - unless anybody has any better suggestions?

This will allow you to use Ninject to implement the unit of work patter leveraging Ninject's InRequestScope().OnDeactivation functionality. I'm aware that the UserManager class has a per request lifetime, but didn't know the most the most appropriate way to commit any outstanding transactions on request finish.


Note This was for WebApi (using System.Web.Http)

Okay so I kind of cheated by using stuff from System.Web which is the namespace we're suppose to be weening ourselves off of, but while its still used, why not.

Firstly, I use some helpers from this SO question:

Configuring Ninject with Asp.Net MVC & Web Api

Within which, the resolver is registered with System.Web's global configuration. Thus, I just go grab it when I need it:

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)    {        var repository = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver                            .GetService(typeof(Data.Repositories.UserRepository)) as Data.Repositories.UserRepository;        var manager = new ApplicationUserManager(repository);...

Note: I use the term Repository over Store since it matches the well-known pattern, more understandable to most people.

And the Startup.Auth looks like this, I basically move the Ninject init into here so its done in time:

    public void ConfigureAuth(IAppBuilder app)    {        // Dependency Injection        Evoq.AppName.Configuration.Ninject.NinjectHttpContainer.RegisterAssembly();        // Configure the db context and user manager to use a single instance per request        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);...

I did also use a method similar to the OP where I 'attached' a callback to get the IKernel but while this keeps it all OWINy, the problem with this approach is that you have to call owinContextThing.Get<IKernel>() which means referencing Ninject deeper in my code.

There were ways around it, but it started getting more complex than my solution above.

Additional Note

This is the Identity Framework code that registers the callback. Note the call to app.GetDataProtectionProvider which is essentially the thing we originally needed to make a UserTokenProvider.

    /// <summary>    /// Registers a callback that will be invoked to create an instance of type T that will be stored in the OwinContext which can fetched via context.Get    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="app"></param>    /// <param name="createCallback"></param>    /// <returns></returns>    public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<IdentityFactoryOptions<T>, IOwinContext, T> createCallback) where T : class,IDisposable {        if (app == null) {            throw new ArgumentNullException("app");        }        if (createCallback == null) {            throw new ArgumentNullException("createCallback");        }        app.Use(typeof(IdentityFactoryMiddleware<T, IdentityFactoryOptions<T>>),            new IdentityFactoryOptions<T>() {                DataProtectionProvider = app.GetDataProtectionProvider(),                Provider = new IdentityFactoryProvider<T>() {                    OnCreate = createCallback                }            });        return app;    }

I've looked and looked and reflected the libs and cannot find that method! If I knew how that worked, potentially we could find another way to create a token, i.e. wouldn't need the options instance.