I´m trying to create some users in a seed method, but no luck.
I had used this example: How to add role and Users in asp.net mvc 5? but the UserManager
seams to be instantiate in a different way.
Looking in the AccountController
, this instance is loaded in the constructor:
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
BennerCliContext applicationDbContext)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_applicationDbContext = applicationDbContext;
}
But my seed method lies on the MyDbContext
class.
How can I access the UserManager
instance from there?
There are another ways to create users in vnext ?
After @Vladislav Karamfilov suggestion:
Since user manager already had been registered in ASP.NET5's service provider you could easily get instance of user manager by calling following method:
var userManager=new HttpContextAccessor()
.HttpContext.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With