How do I inject Identity classes with Ninject?

落爺英雄遲暮 提交于 2019-12-22 04:51:20

问题


I'm trying to use UserManager in a class, but I'm getting this error:

Error activating IUserStore{ApplicationUser}
No matching bindings are available, and the type is not self-bindable.

I'm using the default Startup.cs, which sets a single instance per request:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

I'm able to get the ApplicationDbContext instance, which I believe is getting injected by Owin (Is that true?):

public class GenericRepository<T> : IGenericRepository<T> where T : class
{
    private ApplicationDbContext context;

    public GenericRepository(ApplicationDbContext context)
    {
        this.context = context;
    }
}

But I can't do the same with UserManager (It throws the error shown before):

public class AnunciosService : IAnunciosService
{
    private IGenericRepository<Anuncio> _repo;
    private ApplicationUserManager _userManager;

    public AnunciosService(IRepositorioGenerico<Anuncio> repo, ApplicationUserManager userManager)
    {
        _repo = repo;
        _userManager = userManager;
    }
}

The controller uses the UserManager like this:

public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

I'm using ninject to inject my other classes, but how do I inject the UserManager with it's dependencies and avoid using it like that in my controllers?


回答1:


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.




回答2:


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;
            });


来源:https://stackoverflow.com/questions/29089877/how-do-i-inject-identity-classes-with-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!