How to create ApplicationUser by UserManager in Seed method of ASP .NET MVC 5 Web application

Yoda picture Yoda · Aug 20, 2014 · Viewed 36.7k times · Source

I can create users in the old way:

 var users = new List<ApplicationUser> { 
                        new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]",  SecurityStamp = Guid.NewGuid().ToString()},
                        new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]",  SecurityStamp = Guid.NewGuid().ToString()}
                        };

users.ForEach(user => context.Users.AddOrUpdate(user));

context.SaveChanges();

but I want to do it the ASP.NET MVC 5.1 way using UserManager. I peeked how the Register POST method looks in AccountController:

 public async Task<ActionResult> Register(RegisterViewModel model) {
            if (ModelState.IsValid) {
                var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded) { [...]

so I tried do the same:

var user =  new ApplicationUser() { Email = "[email protected]", 
                                    UserName = "[email protected]"};
IdentityResult result =  UserManager.CreateAsync(user, "abcwq12312!P");

but I get this:

enter image description here

also If I just type UserManager. VS2013 does not shows any methods on the list.

So how to add user in this way?

EDIT1:

enter image description here

Answer

Yoda picture Yoda · Aug 20, 2014

Ok so to create user CreateAsync is unnecessary the problem was somewhere else. One should use ApplicationUserManager not UserManager(this one did not add anything to the database).

 var store = new UserStore<ApplicationUser>(context);
 var manager =  new ApplicationUserManager(store);
 var user = new ApplicationUser() { Email = "[email protected]", UserName = "[email protected]" };
 manager.Create(user, "TestPass44!");