How to create ApplicationUser by UserManager in Seed method of ASP .NET MVC 5 Web application
Solution 1
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!");
Solution 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 aClass
as I've expected. Its a property of typeUserManager<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.
Yoda
If you have a question about Bonjour in .NET and AXIS SDK I am the guy. I HATE telerik.
Updated on July 17, 2022Comments
-
Yoda almost 2 years
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 theRegister
POST method looks inAccountController
: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:
also If I just type
UserManager.
VS2013 does not shows any methods on the list.So how to add user in this way?
EDIT1:
-
Horizon_Net over 9 yearsIs 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).
-
Yoda over 9 years@Horizon_Net I want to learn how to do it.
-
Bart Calixto over 9 yearspossible duplicate of Unable to access CreateAsync in User Manager
-
-
Yoda over 9 yearsTried that before please look at edit in OP. I also added async in method description.
-
Bart Calixto over 9 yearsAfter better research, I think here is your answer : stackoverflow.com/questions/24081586/…
-
Yoda over 9 yearsI posted the answer. Thank you for your help.
-
MUHAMMAD TASADDUQ ALI about 7 yearsHi @Yoda, please explain the value of context used in above code "var store = new UserStore<ApplicationUser>(context)" . Thanks
-
EverPresent over 6 yearsI 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.
-
Nugs over 5 yearsYou're a life saver!