Seeding user doesn't allow user to login in ASP.NET Core 8 Web API - endpoints do allow them to sign in

30 Views Asked by At

When I seed my users into the database at first migration time, I cannot log in at all via the new ASP.NET Core 8 API Identity endpoints.

However, if I create the same user using the new endpoint /register Swagger API and then log in, it works after confirming the user's email and so forth.

But if I merely set [EmailConfirmed] to True via SQL in my seed script, it's not working and not allowing my user in. I replaced my real domain with 'testdomain' for security.

private void SeedUsers(ModelBuilder modelBuilder)
{
    //Seeding a  'Administrator' role to AspNetRoles table
    modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole
        { Id = "2c5e174e-3b0e-446f-86af-483d56fd7210", 
          Name = "Administrator", 
          NormalizedName = "ADMINISTRATOR".ToUpper() });
    modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole
        { Id = "8cc980c3-8643-4166-8dcb-de924036ec6b", 
          Name = "Staff",
          NormalizedName = "Staff".ToUpper() });

    // a hasher to hash the password before seeding the user to the db
    var hasher = new PasswordHasher<IdentityUser>();

    // Seeding the Admin user to AspNetUsers table
    modelBuilder.Entity<IdentityUser>().HasData(
      new IdentityUser
      {
          Id = "8e445865-a24d-4543-a6c6-9443d048cdb9", // primary 
          UserName = "[email protected]",
          Email = "[email protected]",
          NormalizedEmail = "[email protected]",
          NormalizedUserName = "ADMIN",
          LockoutEnabled = false,
          EmailConfirmed = true,
          PasswordHash = hasher.HashPassword(null, "Test12345!@")
       }
    );

    modelBuilder.Entity<IdentityUserRole<string>>().HasData(
        new IdentityUserRole<string>
            {
                RoleId = "2c5e174e-3b0e-446f-86af-483d56fd7210",
                UserId = "8e445865-a24d-4543-a6c6-9443d048cdb9"
            });

    // Seeding the relation staff Member between our user and role to AspNetUserRoles table

    modelBuilder.Entity<IdentityUser>().HasData(
         new IdentityUser
             {
                 Id = "d18e858a-c38d-4083-99b6-c5697b81d7cd", // primary 
                 UserName = "Staff",
                 Email = "[email protected]",
                 NormalizedEmail = "[email protected]",
                 NormalizedUserName = "STAFF"
                 EmailConfirmed = true,
                 PasswordHash = hasher.HashPassword(null, "Test12345!@")
             });

      modelBuilder.Entity<IdentityUserRole<string>>().HasData(
      new IdentityUserRole<string>
      {
          RoleId = "8cc980c3-8643-4166-8dcb-de924036ec6b",
          UserId = "d18e858a-c38d-4083-99b6-c5697b81d7cd"
      }
  );
}

But if I register my user through here and click the physically click here to activate in email it works

enter image description here

enter image description here

Can someone tell me what am doing wrong in my seeding?

1

There are 1 best solutions below

0
c-sharp-and-swiftui-devni On

So the fix was an error in the casing of some the login credentials. But also I had not been populating the securitystamp similar ro this post.

Even though the seed entered a value their physical adding it to seed code seemed to fix issues

https://github.com/aspnet/AspNetCore/issues/10689

I set it to a guid at time of seeding