Reputation: 128
I ran into a problem with my AuthenticationController, for which I use Identity 2.0. The task is simple, but there's something I cannot understand about Ninject and its bindings.
I want to bind the UserManager to UserStore and the DBContext, but I can't figure out how to do it. Also, the more important question is in which scope I have to set the bindings for UserManager and UserStore.
Also, I have this method in my AuthenticationController:
private void UserValidator(UserManager<User> usermanager)
{
usermanager.UserValidator = new UserValidator<User>(usermanager)
{
AllowOnlyAlphanumericUserNames = true
};
}
I don't want to create a new UserValidator and to call this method in other method (or in the constructor), but to bind this method with my UserManager when it's created. How can I do this with Ninject?
Upvotes: 2
Views: 2037
Reputation: 1310
UserManager, UserStore and DBContext are creating dependecy chain so you must type something like that
kernel.Bind<IDBContext>().To<DBContext>().InRequestScope(); //It's good practice to use interface
kernel.Bind<DBContext>().ToSelf().InRequestScope(); //You can also do it this way
kernel.Bind<IUserStore<User>>().To<UserStore<User>>()
.InRequestScope()
.WithConstructorArgument("context", kernel.Get<IDBContext>());
kernel.Bind<UserManager<User>>().ToSelf()
.InRequestScope();
I think it's better to stay with separate method for UserValidator because it's only used when registering new user.
Upvotes: 4