SidC
SidC

Reputation: 3213

How to Seed Database with Multiple Users assigned to Roles?

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 = "[email protected]",
        };
        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 = "[email protected]",
        };
        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 = "[email protected]",

        };
        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 = "[email protected]",
        };
        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 = "[email protected]",
        };
        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?

Upvotes: 0

Views: 465

Answers (2)

nocturns2
nocturns2

Reputation: 661

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", "[email protected]", "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.

Upvotes: 1

Steve Greene
Steve Greene

Reputation: 12324

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?

Upvotes: 0

Related Questions