2017-09-17 1 views
0

こんにちは私はIDで作成されたいくつかのテーブルを初期化する方法を考えていましたが、特定のロールに属するアクセス権を持つセキュリティモジュールを実装していましたので、PermissionとRolePermissionテーブルを追加しなければなりませんでした。それらをitialilizeする方法の方法は、ここで私はアイデンティティに許可を作成することができ、クラスとRolePermissionにテーブルを追加私のコードASP.NET Identity C#で作成された新しいテーブルを初期化するにはどうすればよいですか?

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; } 

} 
} 

そして、ここでは、私が試したシード方法でユーザーおよびロールテーブルを初期化私のコードだのです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 = "[email protected]", 
       EmailConfirmed = true, 
       FirstName = "Taiseer", 
       LastName = "Joudeh", 
       Level = 1, 
       JoinDate = DateTime.Now.AddYears(-3) 
      }; 

      manager.Create(user, "[email protected]!"); 

      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" 
       } 

      }; 

     } 
    } 
+0

許可マネージャーを実装しようとした場合、すでにあるコードを投稿してください。 –

答えて

0

あなたはIdentityDbContextクラスの仮想プロパティとしてすべてのクラスを追加する必要があり、それは、あなたが上記示したConfigurationクラスでDbContext から継承されたパッケージマネージャコンソールで、その後

AutomaticMigrationsEnabled = true; 

に設定されています

update-database -verbose 
+0

Tあなたの答えのハンクスはとても有用だった – Brandon

関連する問題