看代碼基本就都能看懂了,增加了兩個用戶詳細信息的表,角色表增加兩個字段頁面中實現樹形顯示。
//IdentityModels.cs using System.Data.Entity; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; namespace WebDemo20150801.Models { public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); return userIdentity; } public virtual UserSetting UsersSetting { get; set; }//增加兩個表用來顯示用戶數據 public virtual UserInfo UsersInfo { get; set; } } public class UserSetting { public string Id { get; set; } public string Theme { get; set; } } public class UserInfo { public string Id { get; set; } public string TrueName { get; set; } public string Phone { get; set; } } public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public ApplicationRole(string name, string description) : this(name) { this.Description = description; } public string Description { get; set; }//角色表里新增加的字段 public string ParentRole { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public new IDbSet<ApplicationRole> Roles { get; set; }//一定要重寫這個方法,不然能用,網頁中也能獲取數據,就是代碼里點不出來~~ public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } }
這是MVC5,MVC6看起來舒服多了,還能方便的修改ID為數字類型。
public class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string> { }
但是現在的版本需要這樣
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
所以我放棄吧ID改成數字了。
這有ID變為數字的代碼
https://github.com/TypecastException/AspNet-Identity-2-With-Integer-Keys/blob/master/IdentitySample/Models/IdentityModels.cs