How to programmatically create users in asp.net 5 using identity

I am trying to create some users in a seed method, but no luck.

I used this example: How to add a role and Users in asp.net mvc 5? , but UserManager seams will be created differently.

In the AccountController this instance is loaded into 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 in the MyDbContext class.

How can I access an instance of UserManager from there?

Are there other ways to create users in vnext?

Update

After the proposal of Vladislav Karamfilov:

enter image description here

+6
source share
2 answers

Since the user manager has already been registered with the ASP.NET5 service provider, you can easily get the user manager instance by calling the following method:

  var userManager=new HttpContextAccessor() .HttpContext.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>)); 
+5
source

You must create an instance of UserManager<ApplicationUser> , and then use the CreateIdentity method to create the ApplicationUser object. However, if you assign the administrator role to the created user, and your code is publicly available, be very careful not to endanger the security of your site .

0
source

All Articles