How do I inject Identity classes with Ninject?

爷,独闯天下 提交于 2019-12-05 04:20:55

I injected It like this

kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();

And now It's working as it should.

OP's answer didn't work for me, as I was using a custom ApplicationUser class that has long as a key instead of string .

Hence, I created a generic static method the would get the OwinContext from the current HttpContext and return the desired concrete implementation.

private static T GetOwinInjection<T>(IContext context) where T : class
{
            var contextBase = new HttpContextWrapper(HttpContext.Current);
            return contextBase.GetOwinContext().Get<T>();
}

I then used GetOwinInjection method for injection like this:

kernel.Bind<ApplicationUserManager>().ToMethod(GetOwinInjection<ApplicationUserManager>);

kernel.Bind<ApplicationSignInManager>().ToMethod(GetOwinInjection<ApplicationSignInManager>);

If you are also using IAuthenticationManger, you should inject it like this:

kernel.Bind<IAuthenticationManager>().ToMethod(context =>
            {
                var contextBase = new HttpContextWrapper(HttpContext.Current);
                return contextBase.GetOwinContext().Authentication;
            });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!