ASP.NET Identity “角色-許可權”管理 4

來源:互聯網
上載者:User

標籤:

1.1.       User-Role分析

想必大家已經注意到了Microsoft.AspNet.Identity.EntityFramework是對Microsoft.AspNet.Identity.Core的EF實現,微軟是如何處理IdentityUser與IdentityRole的關係?因兩者為多對多關係,會在關係型資料庫增加一張關聯表,故增加IdentityUserRole,並在IdentityUser與IdentityRole中添加IdentityUserRole列表,代碼如下所示。

public class IdentityUserRole<TKey>

{

    public virtual TKey RoleId { get; set; }

       

    public virtual TKey UserId { get; set; }

}

IdentityUser

public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey> where TLogin: IdentityUserLogin<TKey> where TRole: IdentityUserRole<TKey> where TClaim: IdentityUserClaim<TKey>

{

    public IdentityUser()

    {

        this.Roles = new List<TRole>();

    }

    public ICollection<TRole> Roles { virtual get; private set; }

    其它代碼省略….

}

IdentityRole

public class IdentityRole<TKey, TUserRole> : IRole<TKey> where TUserRole: IdentityUserRole<TKey>

{

    public IdentityRole()

    {

        this.Users = new List<TUserRole>();

    }

       

    public TKey Id { get; set; }

       

    public string Name { get; set; }

       

    public ICollection<TUserRole> Users { virtual get; private set; }

}

EF分別配置IdentityUser、IdentityRole與IdentityUserRole的1對多關係。

public class IdentityDbContext: DbContext

{

    public IdentityDbContext() : this("DefaultConnection")

    {

    }

       

    protected override void OnModelCreating(DbModelBuilder modelBuilder)

    {

        if (modelBuilder == null)

        {

            throw new ArgumentNullException("modelBuilder");

        }

        EntityTypeConfiguration<TUser> configuration = modelBuilder.Entity<TUser>().ToTable("AspNetUsers");

        configuration.HasMany<TUserRole>(u => u.Roles).WithRequired().HasForeignKey<TKey>(ur => ur.UserId);

        IndexAttribute indexAttribute = new IndexAttribute("UserNameIndex") {

            IsUnique = true

        };

        configuration.Property((Expression<Func<TUser, string>>) (u => u.UserName)).IsRequired().HasMaxLength(0x100).HasColumnAnnotation("Index", new IndexAnnotation(indexAttribute));

        configuration.Property((Expression<Func<TUser, string>>) (u => u.Email)).HasMaxLength(0x100);

        modelBuilder.Entity<TUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

        EntityTypeConfiguration<TRole> configuration2 = modelBuilder.Entity<TRole>().ToTable("AspNetRoles");

        IndexAttribute attribute2 = new IndexAttribute("RoleNameIndex") {

            IsUnique = true

        };

        configuration2.Property((Expression<Func<TRole, string>>) (r => r.Name)).IsRequired().HasMaxLength(0x100).HasColumnAnnotation("Index", new IndexAnnotation(attribute2));

        configuration2.HasMany<TUserRole>(r => r.Users).WithRequired().HasForeignKey<TKey>(ur => ur.RoleId);

    }

       

    public virtual IDbSet<TRole> Roles { get; set; }

       

    public virtual IDbSet<TUser> Users { get; set; }

}

模仿上述設計,實現Role-Permission關係。

ASP.NET Identity “角色-許可權”管理 4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.