[英]ASP.NET Identity 3.0 Entity Framework Database Seeding
I'm playing with ASP.NET 5 RC1 and am using Identity 3.0 to provide authentication using usernames/passwords, using the built in ASPNET schema and EF7. 我正在使用ASP.NET 5 RC1,正在使用Identity 3.0通过内置的ASPNET架构和EF7使用用户名/密码提供身份验证。
For testing purposes, I'm trying to seed the database with roles and users, using a static method called from Startup.cs. 为了进行测试,我尝试使用从Startup.cs调用的静态方法为数据库分配角色和用户。 The following code works for adding roles, but not for users.
以下代码可用于添加角色,但不适用于用户。 (I couldn't find any sample code for using the new RoleManager and UserManager classes in Identity 3).
(我找不到在Identity 3中使用新的RoleManager和UserManager类的任何示例代码)。 Am I using the correct method call to UserManager here?
我在这里使用对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");
}
I am not sure why it is not working for you. 我不确定为什么它对您不起作用。 It could be from where you are calling the seeding method from?
可能是从哪里调用种子方法的? Eg It was duplicating user accounts when I called it from an action in a controller for example.
例如,当我从控制器中的某个动作调用用户帐户时,它就是在复制用户帐户。
The database seeding worked for me with RC1 when I followed the instructions on this link . 当我按照此链接上的说明进行操作时,数据库种子对RC1很有帮助。
The problem is the CreateAsync call in the user creation block. 问题是用户创建块中的CreateAsync调用。 When you call manager.CreateAsync(user, "chris"), you are trying to create the user with UserName "chris" with password "chris".
当您调用manager.CreateAsync(user,“ chris”)时,您正在尝试使用用户名“ chris”和密码“ chris”创建用户。 It is possible that such password doesn't meet the security restrictions of the Identity Framework.
这样的密码可能不符合Identity Framework的安全性限制。
In any case, the solution is simple: 无论如何,解决方案都很简单:
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");
I checked the existing boilerplate code in the ManageController. 我检查了ManageController中现有的样板代码。 It seems that need to set modifier async for the Seeding method, and use the await where appropriate to get the seeding done.
似乎需要为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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.