Pulse7
7/13/2017 - 5:02 PM

Using user manager and user store for seeding + roles

Using user manager and user store for seeding + roles

protected override void Seed(Auth.Models.ApplicationDbContext context)
{
    if (!context.Users.Any(u=>u.UserName=="sallen"))
    {
        RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>();
        RoleManager<IdentityRole> roleManager = new RoleManager<IdentityRole>(roleStore);

        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
        UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(store);
        ApplicationUser user = new ApplicationUser { UserName="sallen"};
        userManager.Create(user, "123456");

        roleManager.Create(new IdentityRole() { Name = "admin" });
        userManager.AddToRole(user.Id, "admin");
    }
}
protected override void Seed(Auth.Models.ApplicationDbContext context)
{
    if (!context.Users.Any(u=>u.UserName=="sallen"))
    {
        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
        UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(store);
        var user = new ApplicationUser { UserName="sallen"};
        manager.Create(user, "123456");
    }
}