How do I inject Identity classes with Ninject?
Asked Answered
L

2

9

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?

Lime answered 17/3, 2015 at 1:30 Comment(0)
L
13

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.

Lime answered 17/3, 2015 at 3:50 Comment(3)
Nice solution. It's working on my side as well. Thanks.Jeer
Very helpful, thank you. You should go ahead and accept this as the answer even though you answered your own question.Nl
Do you happen to have a working code example? I've been struggling with this for couple weeks now and can't seem to figure it out.Wandawander
H
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;
            });
Hospitalet answered 20/6, 2016 at 9:36 Comment(1)
Well I'm really stuck, because my key IS a string and OP's answer also does not work, I get exactly the same exception.Anemograph

© 2022 - 2024 — McMap. All rights reserved.