Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inject Identity classes with Ninject?

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?

like image 359
Carlos Martinez Avatar asked Mar 17 '15 01:03

Carlos Martinez


2 Answers

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.

like image 50
Carlos Martinez Avatar answered Nov 07 '22 05:11

Carlos Martinez


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;
            });
like image 40
xabush Avatar answered Nov 07 '22 05:11

xabush