Search code examples
entity-frameworkasp.net-mvc-5seedasp.net-roles

Seed an admin role with password in MVC 5?


I'm trying to seed an admin role in MVC5 using Entity Framework and it seems that we're no longer using WebSecurity and SimpleRoleProvider.

I found a great post titled: MVC5 Seed Roles and Users.

I read through this and tried implementing it but I'm having trouble in two spots. In the example provided they only show how to add a username, while the site requires an email and password to register.

enter image description here

here's how i used to do it in MVC4:

protected override void Seed(PhotoAlbum.Models.UsersContext context)
{
    seedUserAccounts();
}

private void seedUserAccounts()
{
    WebSecurity.InitializeDatabaseConnection("UsersContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);

    SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider;

    if (!roles.RoleExists("admin"))
    {
        roles.CreateRole("admin");
    }

    SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider;

    if (membership.GetUser("account", false) == null)
    {
        membership.CreateUserAndAccount("account", "pass");
    }

    if (!roles.IsUserInRole("account", "admin"))
    {
        roles.AddUsersToRoles(new[] { "account" }, new[] { "admin" });
    }
 }

My question:

I'd like to know how to alter the method below to allow for an email address and password.

   protected override void Seed(BlogEngine.Models.ApplicationDbContext context)
   {

       if (!context.Roles.Any(r => r.Name == "Admin"))
       {
           var store = new RoleStore<IdentityRole>(context);
           var manager = new RoleManager<IdentityRole>(store);
           var role = new IdentityRole { Name = "Admin" };


           manager.Create(role);
       }

       if (!context.Users.Any(u => u.UserName == "myUsername"))
       {
           var store = new UserStore<ApplicationUser>(context);
           var manager = new UserManager<ApplicationUser>(store);
           var user = new ApplicationUser { UserName = "myUsername" };

           manager.Create(user, "ChangeItAsap!");
           manager.AddToRole(user.Id, "Admin");
        }
    }

Solution

  • As Steve Greene said above, I was pretty much there already... I did make a few changes. Here's my final solution:

      // makes an admin role if one doesn't exist
      if (!context.Roles.Any(r => r.Name == "Admin"))
      {
            var store = new RoleStore<IdentityRole>(context);
            var manager = new RoleManager<IdentityRole>(store);
            var role = new IdentityRole { Name = "Admin" };
    
    
            manager.Create(role);
       }
       // if user doesn't exist, create one and add it to the admin role
       if (!context.Users.Any(u => u.UserName == "admin"))
       {
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store);
            var user = new ApplicationUser { UserName = "[email protected]", Email = [email protected]" };
    
            manager.Create(user, "password");
            manager.AddToRole(user.Id, "Admin");
        }