ASP.NET Identity 3.0实体框架数据库种子

时间:2015-12-15 19:33:02

标签: c# asp.net asp.net-mvc entity-framework asp.net-identity

我正在使用ASP.NET 5 RC1并使用Identity 3.0使用内置的ASPNET架构和EF7使用用户名/密码提供身份验证。

出于测试目的,我正在尝试使用从Startup.cs调用的静态方法为角色和用户播种数据库。以下代码适用于添加角色,但不适用于用户。 (我在Identity 3中找不到任何使用新RoleManager和UserManager类的示例代码)。我在这里使用正确的方法调用UserManager吗?

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

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

        if (!context.Users.Any(u => u.UserName == "darin"))
        {
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store, null, null, null, null, null, null, null, null, null);
            var user = new ApplicationUser { UserName = "darin", Email = "dherle@gmail.com" };
            manager.CreateAsync(user, "admin");
            manager.AddToRoleAsync(user, "Admin");
        }

        if (!context.Users.Any(u => u.UserName == "chris"))
        {
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store, null, null, null, null, null, null, null, null, null);
            var user = new ApplicationUser { UserName = "chris", Email = "dan@gmail.com" };
            manager.CreateAsync(user, "chris");
            manager.AddToRoleAsync(user, "User");
        }

3 个答案:

答案 0 :(得分:0)

我不确定为什么它不适合你。它可能来自你从哪里调用播种方法?例如。例如,当我从控制器中的操作调用它时,它正在复制用户帐户。

当我按照link上的说明操作时,数据库种子为RC1工作了。

答案 1 :(得分:0)

问题是用户创建块中的CreateAsync调用。当您调用manager.CreateAsync(user,“chris”)时,您尝试使用密码为“chris”的UserName“chris”创建用户。此类密码可能不符合Identity Framework的安全限制。

无论如何,解决方案很简单:

var user = new ApplicationUser { UserName = "chris", Email = "chris@chris.com" };
PasswordHasher<ApplicationUser> ph = new PasswordHasher<ApplicationUser>();
user.PasswordHash = ph.HashPassword(user, "admin3##"); //More complex password
manager.CreateAsync(user);
manager.AddToRoleAsync(user, "admin");

答案 2 :(得分:0)

我检查了ManageController中的现有样板代码。似乎需要为Seeding方法设置修饰符异步,并在适当的地方使用await来完成播种。

    public static async void SeedData(this IApplicationBuilder app)
    {
        var db = app.ApplicationServices.GetService<ApplicationDbContext>();

        db.Database.Migrate();
        //If no role found in the Roles table
        //RoleStore needs using Microsoft.AspNet.Identity.EntityFramework;
        var identityRoleStore = new RoleStore<IdentityRole>(db);
        var identityRoleManager = new RoleManager<IdentityRole>(identityRoleStore, null, null, null, null, null);

            var adminRole = new IdentityRole { Name = "ADMIN" };
        await identityRoleManager.CreateAsync(adminRole);



            var officerRole = new IdentityRole { Name = "OFFICER" };
        await identityRoleManager.CreateAsync(officerRole);




        var userStore = new UserStore<ApplicationUser>(db);
        var userManager = new UserManager<ApplicationUser>(userStore, null, null, null, null, null, null, null, null, null);


        var danielUser = new ApplicationUser { UserName = "DANIEL", Email = "DANIEL@EMU.COM" };
        PasswordHasher<ApplicationUser> ph = new PasswordHasher<ApplicationUser>();
         danielUser.PasswordHash = ph.HashPassword(danielUser, "P@ssw0rd!1"); //More complex password
        await userManager.CreateAsync(danielUser);

        await userManager.AddToRoleAsync(danielUser, "ADMIN");



           var susanUser = new ApplicationUser { UserName = "SUSAN", Email = "SUSAN@EMU.COM" };
           susanUser.PasswordHash = ph.HashPassword(susanUser, "P@ssw0rd!2"); //More complex password
        await userManager.CreateAsync(susanUser);

        await userManager.AddToRoleAsync(susanUser, "ADMIN");



            var randyUser = new ApplicationUser { UserName = "RANDY", Email = "RANDY@EMU.COM" };
        randyUser.PasswordHash = ph.HashPassword(randyUser, "P@ssw0rd!3"); //More complex password
        await userManager.CreateAsync(randyUser);

        await userManager.AddToRoleAsync(randyUser, "OFFICER");






        var thomasUser = new ApplicationUser { UserName = "THOMAS", Email = "THOMAS@EMU.COM" };
      thomasUser.PasswordHash = ph.HashPassword(thomasUser, "P@ssw0rd!4"); //More complex password

        await userManager.CreateAsync(thomasUser);
        await userManager.AddToRoleAsync(thomasUser, "OFFICER");

        var benUser = new ApplicationUser { UserName = "BEN", Email = "BEN@EMU.COM" };
        benUser.PasswordHash = ph.HashPassword(benUser, "P@ssw0rd!5"); //More complex password

        await userManager.CreateAsync(benUser);

        await userManager.AddToRoleAsync(benUser, "OFFICER");


          if (db.Courses.FirstOrDefault() == null)
          {
              Course ditCourse = new Course()
              {
                  CourseAbbreviation = "DIT",
                  CourseName = "DIPLOMA IN INFORMATION TECHNOLOGY",
                  CreatedById = randyUser.Id,
                  UpdatedById = thomasUser.Id
              };
              db.Courses.Add(ditCourse);
              Course dbitCourse = new Course()
              {
                  CourseAbbreviation = "DIPLOMA IN BUSINESS INFORMATION TECHNOLOGY",
                  CourseName = "DBIT",
                  CreatedById = thomasUser.Id,
                  UpdatedById = thomasUser.Id
              };
              db.Courses.Add(dbitCourse);
              Course dismCourse = new Course()
              {
                  CourseAbbreviation = "DISM",
                  CourseName = "DIPLOMA IN INFOCOMM SECURITY MANAGEMENT",
                  CreatedById = thomasUser.Id,
                  UpdatedById = benUser.Id
              };
              db.Courses.Add(dismCourse);

          }

        db.SaveChanges();

        // TODO: Add seed logic here


    }