Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 921
Yoda Avatar asked Aug 20 '14 16:08

Yoda


People also ask

How to get user Role in asp net Identity?

Instead you could try UserManger 's GetRoles method which will return you List<string> of roles user is assigned. But as you mentioned it will be only one role hence you can take first value from the result of GetRoles method.


2 Answers

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!");
like image 113
Yoda Avatar answered Sep 20 '22 14:09

Yoda


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.

like image 42
Bart Calixto Avatar answered Sep 17 '22 14:09

Bart Calixto