I use Ninject on a MVC 5 project with Identity 2.
For rest of data context and controllers using that I have no problems with dependency injection.
For Account controller that uses Identity 2 model I'm getting null UserManager when I try to login:
public class AccountController : Controller
{
private ApplicationUserManager _userManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
public ApplicationUserManager UserManager {
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
What is the proper way to inject required dependencies? I did not create a custom UserManager, it's out of the box model, ApplicationUserManager is as defined in IdentityConfig.cs under App_Start.
As a side note: I'm using Conventions Extension of Ninject:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(
x => x.FromThisAssembly()
.SelectAllClasses()
.BindAllInterfaces()
);
Thanks.
I had the same problem working with Ninject4.0, MVC5.2.3, Identity2.0 and Owin3.0.1, here you should do for working everything properly.
First of all you should get Ninject.Web.Common.OwinHost from NuGet, because if you working with Owin, you want to use NinjectMiddleware.
As you can see, we have a method for injecting all services that we want, in this case we must inject ApplicationUserManager, IUserStore and DbContext. So for RegisterServices in Startup class, we have :
Note: I use long insted of string as a type of all keys on Identity, but using a defualt (string as key) should be the same.
In StartUp.Auth.cs file that contains partial of StartUp class we should change ConfigureAuth method to this :
Note: we get ApplicationUserManager from owin before but now we get it from Ninject kernel.please notice we create a internal static property for DataProtectionProvider and set it from appBuilder, we're going to use this property in IdentityConfig/ApplicationUserManager.(see it later)
Now we must change ApplicationUserManager, we remove code from Create method to constructor with just one dependency with IUserStore, here is the final look:
at last we can use ApplicationUserManager in our Accountcontroller's constructor like this:
Can you try removing the mentioned lines in your question from your code and replace it with the following: