0

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");
        }
    }
Community
  • 1
  • 1
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
  • 1
    You've already got the password set (ChangeItAsap!). You can add the other fields via the initializer: var newUser = new ApplicationUser { UserName = "admin", Email = "newuser@domain.com", PhoneNumber = "6085551234", MustChangePassword = false }; – Steve Greene Apr 27 '15 at 01:34
  • this worked. I'll post the working code in a while unless you'd like to post an answer. thanks again – Dan Beaulieu Apr 27 '15 at 02:55

1 Answers1

2

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 = "me@me.com", Email = me@me.com" };

        manager.Create(user, "password");
        manager.AddToRole(user.Id, "Admin");
    }
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135