![](/img/trans.png)
[英]Implementing asp.net Identity into already existing database with entity framework
[英]ASP.NET Identity 3.0 Entity Framework Database Seeding
我正在使用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");
}
我不確定為什么它對您不起作用。 可能是從哪里調用種子方法的? 例如,當我從控制器中的某個動作調用用戶帳戶時,它就是在復制用戶帳戶。
當我按照此鏈接上的說明進行操作時,數據庫種子對RC1很有幫助。
問題是用戶創建塊中的CreateAsync調用。 當您調用manager.CreateAsync(user,“ chris”)時,您正在嘗試使用用戶名“ chris”和密碼“ 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");
我檢查了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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.