我无法在种子数据中创建管理员

时间:2019-09-16 22:04:42

标签: c# asp.net-mvc

到目前为止我做了什么

public class SeedIdentity
{
    public static async Task Seed(IApplicationBuilder app)
    {   
        var identityContext = app.ApplicationServices.GetRequiredService<ApplicationIdentityDbContext>();
        identityContext.Database.Migrate();  

        if (!identityContext.Roles.Any(i => i.Name == "Admin" && i.Name == "User"))
        {
            var roleStore = new RoleStore<IdentityRole>(identityContext);
            var roleManager = new RoleManager<IdentityRole>(roleStore, null, null, null, null);


            var adminRole = new IdentityRole { Name = "admin" };
            var userRole = new IdentityRole { Name = "user" };
            await roleManager.CreateAsync(adminRole);
            await roleManager.CreateAsync(userRole);
            identityContext.SaveChanges();

        }

        if (!identityContext.Users.Any(i => i.UserName == "akinaldemir"))
        {
                var userStore = new UserStore<ApplicationUser>(identityContext);
                var userManager = new UserManager<ApplicationUser>(userStore, null, null, null,null, null, null, null, null);
                var admin = new ApplicationUser()
                {
                    UserName = "akinaldemir",
                    Email = "akinaldemir@gmail.com"
                };


                await userManager.CreateAsync(admin, "Abc12!jtre");
                await userManager.AddToRoleAsync(admin, "admin");

                identityContext.SaveChanges();


        }
    }
}

0 个答案:

没有答案