MVC Identity : Seed rollen en gebruikers

In Package Manager Console : Enable-Migrations

In Models -> IdentityModel

protected override void Seed(XXXXXXXXXXXXXXXXX.Models.ApplicationDbContext context)
{
    if (!context.Roles.Any(r => r.Name == "Gebruiker"))
    {
        var store = new RoleStore<IdentityRole>(context);
        var manager = new RoleManager<IdentityRole>(store);
        var role = new IdentityRole { Name = "Gebruiker" };

        manager.Create(role);
    }

    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 == "Gebruiker1"))
    {
        var store = new UserStore<ApplicationUser>(context);
        var manager = new UserManager<ApplicationUser>(store);
        var user = new ApplicationUser { UserName = "Gebruiker1" };

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

 

Categories:

Related Article