9

I have this seeder class which is called at the end of the Startup.cs file in the Configure method:

public class UserSeeder
{
    private readonly ApplicationDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;
    public UserSeeder(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
    {
        _context = context;
        _userManager = userManager;
    }

    public async Task Seed()
    {
        if (!await _context.Users.AnyAsync())
        {
            var user = new ApplicationUser()
            {
                UserName = "admin",
                Email = "[email protected]"
            };
            await _userManager.CreateAsync(user, "passwort4admin");
        }
    }
}

The code is executed and I even put a try/catch around the method call but no error happens AND no user is inserted into the database!

Why not?

7
  • 10
    CreateAsync returns a Task<IdentityResult>. Check that result, there might be errors. Commented Mar 17, 2017 at 7:45
  • 1
    What @tmg said. Probably your password is failing the policy check, which you can see when you inspect the identity result. Commented Mar 17, 2017 at 8:19
  • Sorry guys, but I did not check that result due to intellisense problem in my vs2017 which is completely broken! Commented Mar 17, 2017 at 12:26
  • @Tseng Now that I could fix my VS2017 problems, I could see that I missed an Uppercase character :-) Commented Mar 18, 2017 at 12:09
  • @Tseng Good answer, it also helped me. Commented Mar 20, 2019 at 14:02

5 Answers 5

12

The problem is the complexity of the password. Add capital and numbers and symbols the issue will solve

Sign up to request clarification or add additional context in comments.

1 Comment

That's one possible cause, but it depends on how your password requirements are set up. Simply inspect the return value of userManager.CreateAsync() for errors.
5

Behind the scenes the UserManager<> uses a IUserStore did you configure this userstore in the startup.cs in the IOC container?

services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<MyContext, Guid>()
    .AddUserStore<ApplicationUserStore>() //this one provides data storage for user.
    .AddRoleStore<ApplicationRoleStore>()
    .AddUserManager<ApplicationUserManager>()
    .AddRoleManager<ApplicationRoleManager>()
    .AddDefaultTokenProviders();

Comments

3

For those who cannot even get a result to inspect: Make sure you await your seeder method in Program.cs, otherwise the method may fail and not return:

using var scope = app.Services.CreateScope()
await scope.ServiceProvider.GetRequiredService<UserSeeder>().Seed();

Then it will be ready for app.Run();

Comments

2

In my case, it was a space in the user name. It's possible that you have other constraints set up that make the user creation operation illegal.

In most cases, you can obtain explicit information on the exact cause of the error by investigating the message in the returned object from the operation.

IdentityUser userToBe = ...
IdentityResult result = await UserManager.CreateAsync(userToBe);
  if(!result.Succeeded)
    foreach(IdentityError error in result.Errors)
      Console.WriteLine($"Oops! {error.Description} ({error.Code}));

Comments

1

I forgot to set "Username", it cannot be empty. I set UserName=userEmail and it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.