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

2

15

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

Millennium answered 20/8, 2014 at 16:37 Comment(3)
Is there any special reason that you want to user the UserManager class? Normally I would give the advice to use the context in the Seed method (and not the Identity stuff - there's no benefit of its asynchronous methods during the seeding of the database) and the Identity framework in the rest of the application (without any usage of the context).Yoko
@Yoko I want to learn how to do it.Millennium
possible duplicate of Unable to access CreateAsync in User ManagerBlondie
M
16

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!");
Millennium answered 20/8, 2014 at 17:23 Comment(3)
Hi @Yoda, please explain the value of context used in above code "var store = new UserStore<ApplicationUser>(context)" . ThanksCellist
I used the UserManager in the seed method without issues. Just make sure you check for the result.Succeeded = true. If nothing is saved to the database, you might be getting weak password errors.Thinskinned
You're a life saver!Douma
B
2

I dont understand the error you are showing, unless you are providing a custom TUser or TKey in which case would be like :

IdentityResult user = await UserManager.CreateAsync<CustomUser, CustomKey>(user, "abcwq12312!P");

and passing user as your CustomUser instead of ApplicationUser and maybe int if your CustomKey is an int instead of string. (CreateAsync can infer types, I posted there to show them explicitly)

The other problem I see is you are not awaiting the task, you must also add await like :

IdentityResult user = await UserManager.CreateAsync(user, "abcwq12312!P");

Hope this helps.


EDIT:

For completeness I will post the full answer from this question but there is your answer. : Unable to access CreateAsync in User Manager

var result = await UserManager.CreateAsync(user, register.Password);

The UserManager in the above statement is not a Class as I've expected. Its a property of type UserManager<ApplicationUser>.

So, at the beginning just declared a property as

public UserManager<ApplicationUser> UserManager { get; private set; }

And now I can use the Async version for creating users. The following statement works.

var result = await UserManager.CreateAsync(user, register.Password);

I will also flag for possible duplicate.

Blondie answered 20/8, 2014 at 16:52 Comment(3)
Tried that before please look at edit in OP. I also added async in method description.Millennium
After better research, I think here is your answer : #24082086Blondie
I posted the answer. Thank you for your help.Millennium

© 2022 - 2024 — McMap. All rights reserved.