UserManager.CreateAsync .Success always returns false

Thierry

I'm trying to add users programmatically to the AspNetUsers table in my ASP.Net MVC 5.0 app using the following function:

private async Task AddUser(DataImportMember member)
{
    var user = new ApplicationUser
    {
        UserName = member.Email,
        Email = member.Email,
        UserType = UserType.IsMember
    };

    var password = RandomPassword.Generate();
    var result = await this.UserManager.CreateAsync(user, password);
    if (result.Succeeded)
    {
        await this.UserManager.AddToRoleAsync(user.Id, MyApp.IsMember);
    }
}

But whenever I call this.UserManager.CreateAsync, result.Succeed is always false. I've checked the AspNetUsers table and as expected, the user is not added.

Any ideas how I can figure out what's wrong and how to resolve it?

Thanks.

Nkosi

UserManager.CreateAsync returns an IdentityResult that would also contain any errors why the action was not successful. You should check the result error messages to find out why the action failed.

var result = await this.UserManager.CreateAsync(user, password);
if (result.Succeeded) {
    await this.UserManager.AddToRoleAsync(user.Id, MyApp.IsMember);
} else {
    var errors = result.Errors;
    var message = string.Join(", ", errors);
    ModelState.AddModelError("", message);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related