Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

update

After @Vladislav Karamfilov suggestion:

enter image description here

like image 268
Beetlejuice Avatar asked Sep 15 '15 11:09

Beetlejuice


1 Answers

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>));
like image 155
Sam FarajpourGhamari Avatar answered Oct 05 '22 14:10

Sam FarajpourGhamari