15

I can create users in the old way:

 var users = new List<ApplicationUser> { 
                        new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4444@wp.pl", UserName = "informatyka4444@wp.pl",  SecurityStamp = Guid.NewGuid().ToString()},
                        new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4445@wp.pl", UserName = "informatyka4445@wp.pl",  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 = "informatyka4444@wp.pl", 
                                    UserName = "informatyka4444@wp.pl"};
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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    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). – Horizon_Net Aug 20 '14 at 16:40
  • 2
    @Horizon_Net I want to learn how to do it. – Yoda Aug 20 '14 at 16:42
  • possible duplicate of [Unable to access CreateAsync in User Manager](http://stackoverflow.com/questions/24081586/unable-to-access-createasync-in-user-manager) – Bart Calixto Aug 20 '14 at 17:01

2 Answers2

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 = "informatyka4444@wp.pl", UserName = "informatyka4444@wp.pl" };
 manager.Create(user, "TestPass44!");
Eifion
  • 5,403
  • 2
  • 27
  • 27
Yoda
  • 17,363
  • 67
  • 204
  • 344
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.

Community
  • 1
  • 1
Bart Calixto
  • 19,210
  • 11
  • 78
  • 114