Question

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?

Accepted Answer

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.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why