ASP.NET 如何初始化在 Identity C# 中创建的新 table?
ASP.NET How can I initialize a new table made in Identity C#?
大家好我想知道如何初始化一些在身份中创建的表,我已经实现了一个具有属于特定角色的权限的安全模块,所以我不得不添加一个 Permission 和 RolePermission 表,但我不能找不到初始化它们的方法这是我的代码,我在其中添加了 类 允许在 identity
中创建 Permission 和 RolePermission 表
namespace SSMX.Mantra.WebApi.Infrastructure
{
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
public byte Level { get; set; }
[Required]
public DateTime JoinDate { get; set; }
public virtual ICollection<Permission> Permissions { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
//Rest of code is removed for brevity
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string
authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this,
authenticationType);
// Add custom user claims here
return userIdentity;
}
}
public class Permission
{
public int Id { get; set; }
public string Code { get; set; }
public string Description { get; set; }
}
public class RolePermission
{
[Key]
public int IdRolePermission { get; set; }
public int IdPermission { get; set; }
public int IdRole { get; set; }
public virtual Permission Permission { get; set; }
public virtual IdentityRole Role { get; set; }
}
}
这是我在种子方法中初始化 User 和 Role 表的代码,因此我尝试通过实现 PermissionManager 来执行与 User 和 Role 表相同的操作,但我无法提供任何帮助,谢谢你们。
internal sealed class Configuration : DbMigrationsConfiguration<SSMX.Mantra.WebApi.Infrastructure.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(ApplicationDbContext context)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "taiseer.joudeh@gmail.com",
EmailConfirmed = true,
FirstName = "Taiseer",
LastName = "Joudeh",
Level = 1,
JoinDate = DateTime.Now.AddYears(-3)
};
manager.Create(user, "MySuperP@ss!");
if (roleManager.Roles.Count() == 0)
{
roleManager.Create(new IdentityRole { Name = "SuperAdmin" });
roleManager.Create(new IdentityRole { Name = "Admin" });
roleManager.Create(new IdentityRole { Name = "User" });
}
var adminUser = manager.FindByName("SuperPowerUser");
manager.AddToRoles(adminUser.Id, new string[] { "SuperAdmin", "Admin" });
var permissions = new List<Permission>
{
new Permission()
{
Id = 1, Code = "101", Description = "Competencias.CentroDeportivo.Consulta"
},
new Permission()
{
Id = 1, Code = "102", Description = "Competencias.CentroDeportivo.Eliminar"
},
new Permission()
{
Id = 1, Code = "102", Description = "Competencias.CentroDeportivo.Crear"
}
};
}
}
您需要在 IdentityDbContext class 中添加所有 classes 作为虚拟属性,这是从 DbContext 继承的
在上面显示的配置 class 中,设置
AutomaticMigrationsEnabled = true;
然后在程序包管理器控制台中 运行 迁移命令
update-database -verbose
大家好我想知道如何初始化一些在身份中创建的表,我已经实现了一个具有属于特定角色的权限的安全模块,所以我不得不添加一个 Permission 和 RolePermission 表,但我不能找不到初始化它们的方法这是我的代码,我在其中添加了 类 允许在 identity
中创建 Permission 和 RolePermission 表namespace SSMX.Mantra.WebApi.Infrastructure
{
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
public byte Level { get; set; }
[Required]
public DateTime JoinDate { get; set; }
public virtual ICollection<Permission> Permissions { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
//Rest of code is removed for brevity
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string
authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this,
authenticationType);
// Add custom user claims here
return userIdentity;
}
}
public class Permission
{
public int Id { get; set; }
public string Code { get; set; }
public string Description { get; set; }
}
public class RolePermission
{
[Key]
public int IdRolePermission { get; set; }
public int IdPermission { get; set; }
public int IdRole { get; set; }
public virtual Permission Permission { get; set; }
public virtual IdentityRole Role { get; set; }
}
}
这是我在种子方法中初始化 User 和 Role 表的代码,因此我尝试通过实现 PermissionManager 来执行与 User 和 Role 表相同的操作,但我无法提供任何帮助,谢谢你们。
internal sealed class Configuration : DbMigrationsConfiguration<SSMX.Mantra.WebApi.Infrastructure.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(ApplicationDbContext context)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "taiseer.joudeh@gmail.com",
EmailConfirmed = true,
FirstName = "Taiseer",
LastName = "Joudeh",
Level = 1,
JoinDate = DateTime.Now.AddYears(-3)
};
manager.Create(user, "MySuperP@ss!");
if (roleManager.Roles.Count() == 0)
{
roleManager.Create(new IdentityRole { Name = "SuperAdmin" });
roleManager.Create(new IdentityRole { Name = "Admin" });
roleManager.Create(new IdentityRole { Name = "User" });
}
var adminUser = manager.FindByName("SuperPowerUser");
manager.AddToRoles(adminUser.Id, new string[] { "SuperAdmin", "Admin" });
var permissions = new List<Permission>
{
new Permission()
{
Id = 1, Code = "101", Description = "Competencias.CentroDeportivo.Consulta"
},
new Permission()
{
Id = 1, Code = "102", Description = "Competencias.CentroDeportivo.Eliminar"
},
new Permission()
{
Id = 1, Code = "102", Description = "Competencias.CentroDeportivo.Crear"
}
};
}
}
您需要在 IdentityDbContext class 中添加所有 classes 作为虚拟属性,这是从 DbContext 继承的 在上面显示的配置 class 中,设置
AutomaticMigrationsEnabled = true;
然后在程序包管理器控制台中 运行 迁移命令
update-database -verbose