Search code examples
asp.net-mvc-5visual-studio-2015asp.net-identityentity-framework-core

How to access ASP.NET UserManager


I am looking for the correct way to get access to the UserManager<MyUser> object outside of a controller using the most recent incarnation of ASP.NET MVC 5 and Entity Framework 7.

I want to seed my database with users in my seed class, but I have been unable to find a way to access the UserManager object to do so.

The Visual Studio 2015 Update 1 default ASP.NET template reveals the statement app.UseIdentity() in the Startup.Configure method while the Account controller accepts the UserManager<MyUser> in its constructor.

How do I access the UserManager in other classes so that I can create seed users?


Solution

  • The UserManager is registered with the built-in dependency injection. You can add

    UserManager<ApplicationUser> userManager
    

    to the constructor of the consuming class, and you should be able to use it. Assuming ASP.NET is responsible for the construction of that class.