【问题标题】:ASP.NET Identity 3.0 Entity Framework Database SeedingASP.NET Identity 3.0 实体框架数据库种子
【发布时间】:2016-03-21 18:01:50
【问题描述】:

我正在使用 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");
        }

【问题讨论】:

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


    【解决方案1】:

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

    当我按照link 上的说明进行操作时,RC1 的数据库播种对我有用。

    【讨论】:

    • 这更像是一条评论。
    【解决方案2】:

    问题在于用户创建块中的 CreateAsync 调用。当您调用 manager.CreateAsync(user, "chris") 时,您正在尝试使用用户名“chris”和密码“chris”创建用户。这样的密码可能不符合身份框架的安全限制。

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

    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");

    【讨论】:

      【解决方案3】:

      我检查了 ManageController 中现有的样板代码。似乎需要为 Seeding 方法设置修饰符 async,并在适当的地方使用 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
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        相关资源
        最近更新 更多