0

I'm using ASP.NET MVC with Identity to secure my web application. I have created a "canEdit" role and have 5 users that I want to seed in the database.

I originally tried this as:

bool AddUserAndRole(ERPWAG.Models.ApplicationDbContext context)
    {
        IdentityResult ir;
        var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        ir = rm.Create(new IdentityRole("canEdit"));

        var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var user = new ApplicationUser()
        {
            UserName = "user1@email.com",
        };
        ir = um.Create(user, "password");
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user.Id, "canEdit");
        return ir.Succeeded;

        var user2 = new ApplicationUser()
        {
            UserName = "user2@email.com",
        };
        ir = um.Create(user, "password");
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user2.Id, "canEdit");
        return ir.Succeeded;

        var user3 = new ApplicationUser()
        {
            UserName = "user3@email.com",

        };
        ir = um.Create(user, "password");
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user3.Id, "canEdit");
        return ir.Succeeded;

        var user4 = new ApplicationUser()
        {
            UserName = "user4@email.com",
        };
        ir = um.Create(user, "password!");
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user4.Id, "canEdit");
        return ir.Succeeded;

        var user5 = new ApplicationUser()
        {
            UserName = "user5@email.com",
        };
        ir = um.Create(user, "password");
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user5.Id, "canEdit");
        return ir.Succeeded;
    }

I then called AddUserAndrole(context) in my Seed method of Configuration.cs, called update-database and published.

My users were unable to login to the site. What is the best way to pre-populate the identity database with these users and associate them with the role?

SidC
  • 3,175
  • 14
  • 70
  • 132

2 Answers2

1

With this you can pass a single user to create a user account and assign them to a role:

    bool CreateDefaultUsers(YourApp.Models.ApplicationDbContext context, string uname, string uemail, string upass, string urole)
    {
        IdentityResult ir;
        var um = new UserManager<ApplicationUser>(
            new UserStore<ApplicationUser>(context));
        var user = new ApplicationUser()
        {
            UserName = uname,
            Email = uemail,
        };
        ir = um.Create(user, upass);
        if (ir.Succeeded == false)
            return ir.Succeeded;
        ir = um.AddToRole(user.Id, urole);
        return ir.Succeeded;
    }

And call it from your seed method like this:

    ir = CreateDefaultUsers(context, "Bob", "bob@bobswebsite.com", "Password1", "Publisher");

You can probably figure out how to create an array and iterate through your users if you want or just repeat the call 8 times with your user values.

I would also consider what @Steve Greene posted, about the password requirements.

[ UPDATE ]

This example assumes that you are not using an email for the user name. In any case the method is the same.

nocturns2
  • 663
  • 10
  • 17
0

Assuming your code ran and the records were inserted into the tables (check that first), I'm betting your password policy may not allow "password".

Search for your PasswordValidator rules. Mine are:

// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
    RequiredLength = 8,
    RequireNonLetterOrDigit = false,
    RequireDigit = true,
    RequireLowercase = true,
    RequireUppercase = true,
};

So either make your password meet the rule or change the validator.

Here is another link on seeding identity: Seed Entities AND Users, Roles?

Community
  • 1
  • 1
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • Thanks for the answer. I "cleaned" the email addresses and passwords prior to posting my question. So, the passwords are strong with one capital letter, one number, a special character and the required length. – SidC Dec 06 '16 at 22:44
  • The records are in the database? The SignIn result is OK? Where is your problem? – Steve Greene Dec 06 '16 at 23:30