在ASP中播种用户和角色时很奇怪.NET Identity 2.0.1
本文关键字:NET Identity ASP 用户 角色 | 更新日期: 2023-09-27 18:13:47
我发现这件作品不能正常工作:
protected override void Seed(PintxoLista.Models.ApplicationDbContext context)
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
if (!context.Roles.Any(r => r.Name == "WeddingGuest"))
{
var role = new IdentityRole { Name = "WeedingGuest" };
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "BrideGroom"))
{
var role = new IdentityRole { Name = "BrideGroom" };
manager.Create(role);
}
if (System.Diagnostics.Debugger.IsAttached == false)
{
System.Diagnostics.Debugger.Launch();
}
context.Users.AddOrUpdate(i => i.Id,
new ApplicationUser
{
Id = "1",
UserName = "han",
Email = "hansolo1@rebels.com",
PasswordHash = new PasswordHasher().HashPassword("1"),
SecurityStamp = string.Empty
},
new ApplicationUser
{
Id = "2",
UserName = "lando",
Email = "lando@rebels.com",
PasswordHash = new PasswordHasher().HashPassword("1"),
SecurityStamp = string.Empty
});
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
userManager.AddToRole("1", "WeedingGuest");
userManager.AddToRole("2", "BrideGroom");
}
创建了角色,但没有创建用户,然后第一个AddToRole
失败。相反,如果先将用户添加(或更新)到数据库中,则可以正常工作。
protected override void Seed(PintxoLista.Models.ApplicationDbContext context)
{
context.Users.AddOrUpdate(i => i.Id,
new ApplicationUser
{
Id = "1",
UserName = "han",
Email = "hansolo1@rebels.com",
PasswordHash = new PasswordHasher().HashPassword("1"),
SecurityStamp = string.Empty
}, new ApplicationUser
{
Id = "2",
UserName = "lando",
Email = "lando@rebels.com",
PasswordHash = new PasswordHasher().HashPassword("1"),
SecurityStamp = string.Empty
});
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
if (!context.Roles.Any(r => r.Name == "WeddingGuest"))
{
var role = new IdentityRole { Name = "WeedingGuest" };
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "BrideGroom"))
{
var role = new IdentityRole { Name = "BrideGroom" };
manager.Create(role);
}
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
userManager.AddToRole("1", "WeedingGuest");
userManager.AddToRole("2", "BrideGroom");
}
谁能解释这种明显奇怪的行为?为什么必须先创建用户?谢谢你! !
必须创建用户才能添加到角色中。将不存在的用户添加到角色中是不切实际的。
我无法重新创建您的场景,种子函数没有按任何顺序保存数据。但这就是我想出来的。
为什么它说"UserId not found"和动作不工作的原因是因为一切都在同一个事务中运行。
UserManager无法将用户与角色链接,因为用户尚未创建。您需要完成当前事务(用户将被添加到DB中)并启动一个新事务,然后您可以向用户添加角色。
见下面的代码
protected override void Seed(MyProject.Dal.Context.MyProjectDbContext context)
{
context.Users.AddOrUpdate(u => u.Id, new Core.Entities.ApplicationUser{
Id = "1",
UserName = "foo@a.com",
Email = "foo@a.com",
PasswordHash = new PasswordHasher().HashPassword("@edulopezTest"),
SecurityStamp = string.Empty,
Names = "Test",
LastNames = "Admin",
BirthDate = DateTime.Now,
});
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!context.Roles.Any(r => r.Name == "SuperAdmin"))
{
var role = new IdentityRole { Name = "SuperAdmin" };
RoleManager.Create(role);
}
//**********************************************************
//THIS IS ANOTHER WAY TO ADD THE ROLES. IT GIVE SAME RESULTS
//**********************************************************
//context.Roles.AddOrUpdate(r => r.Name,
// new IdentityRole { Name = "SuperAdmin" } // 1
// , new IdentityRole { Name = "Admin" } // 2
// );
var userManager = new UserManager<Core.Entities.ApplicationUser>(new UserStore<Core.Entities.ApplicationUser>(context));
userManager.AddToRole("1", "SuperAdmin");
}
要完成当前事务并开始一个新事务,您只需要从当前上下文中调用"SaveChanges"。
查看下面代码的变化
context.SaveChanges(); // <-- Finish and start a new one
var userManager = new UserManager<Core.Entities.ApplicationUser>(new UserStore<Core.Entities.ApplicationUser>(context));
userManager.AddToRole("1", "SuperAdmin");
我使用这个链接作为参考,但主要是通过做测试。
http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/https://msdn.microsoft.com/en-us/data/jj554735