asp.net core系列 48 Identity 身份模型自定義


一.概述

  ASP.NET Core Identity提供了一個框架,用於管理和存儲在 ASP.NET Core 應用中的用戶帳戶。 Identity添加到項目時單個用戶帳戶選擇作為身份驗證機制。 默認情況下,Identity可以使用的 Entity Framework (EF) Core 數據模型。 本文介紹如何自定義的身份標識模型。

 

  1.1 下面是已經存在的身份模型, 由以下實體類型組成:

實體類型

說明

關系

Users(用戶表) 登錄用戶  

Roles (角色表)

角色

 

UserClaims(用戶聲明表) 用戶擁有的權限 每個Users有多個UserClaims
UserTokens 用戶的身份驗證令牌 每個Users有多個UserTokens
UserLogins 將用戶與登錄相關聯。 每個Users有多個UserLogins
RoleClaims(角色聲明表) 角色擁有的權限 每個Roles有多個RoleClaims
UserRoles 用戶和角色關聯 每個Users有多個Roles

    (1) Users 表

字段名稱

字段類型

描述

Id Guid 主鍵,默認是Guid
UserName Nvarchar(256) 用戶名或郵箱
NormalizedUserName Nvarchar(256) 規范化用戶名,轉成了大寫
Email Nvarchar(256) 郵箱
NormalizedEmail Nvarchar(256) 規范化郵箱名,轉成了大寫
EmailConfirmed bit 驗證郵件確認,默認為false
PasswordHash Nvarchar(max) 密碼哈希
SecurityStamp Nvarchar(max) 安全標記,Guid類型,用戶憑據更改時生成隨機值,如更改用戶名
ConcurrencyStamp Nvarchar(max) 同步標記,Guid類型
PhoneNumber Nvarchar(max) 電話
PhoneNumberConfirmed bit> 電話確認
TwoFactorEnabled bit 雙因子驗證
LockoutEnd datetimeoffset(7) 鎖定的到期日期,null表示沒有鎖定
LockoutEnabled bit 是否可以被鎖定
AccessFailedCount int 登陸失敗的次數, 確定是否鎖定用戶

    

  1.2 默認模型的配置

    Identity定義了許多從DbContext繼承以配置和使用模型的上下文類,此配置是使用上下文類的OnModelCreating方法中的EF Core Code First Fluent API完成的。默認模型結構可以查看Migration文件以及查看模型關系ModelSnapshot文件,但要修改模型不在這里更改。下面是AspNetUsers模型代碼:

    下面是默認模型生成的數據表以及關系: 

  

二.模型自定義

   在EF上下文中當重寫OnModelCreating方法時base.OnModelCreating方法首先調用; 接下來重寫的會覆蓋默認模型配置。

    public class ApplicationDbContext : IdentityDbContext<WebAppIdentityDemoUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Core Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Core Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }

  

   2.1 自定義用戶數據

     在上篇有講過自定義用戶數據,這里在總結下。自定義用戶數據支持通過繼承IdentityUser類。 自定義類命名約定 {Application}User。

     //定義{Application}User擴展類,實現用戶模型
      public class WebAppIdentityDemoUser : IdentityUser
     //使用{Application}User作為上下文的泛型參數的類型:
      public class ApplicationDbContext : IdentityDbContext<WebAppIdentityDemoUser>
      //更新Startup.ConfigureServices以使用新{Application}User類,最后生成遷移,同步數據庫。
       services.AddDefaultIdentity<WebAppIdentityDemoUser>()
               .AddDefaultUI()
               .AddEntityFrameworkStores<ApplicationDbContext>();    

 

  2.2 更改主鍵類型  

     在創建數據庫之后更改PK列的數據類型在許多數據庫系統上都存在問題。更改PK通常涉及刪除和重新創建表。因此,在創建數據庫時,應在初始遷移中指定PK類型。下面是更改主鍵類型步驟:

     (1) 刪除數據庫,命令如下:

        Drop-Database  

    (2) 移除之前生成的遷移,命令如下:

        Remove-Migration

    (3) 修改user,role表主鍵類型,以及相關代碼改動

    // 用戶表設置主鍵為Int
    public class WebAppIdentityDemoUser : IdentityUser<int>
    {
        /// <summary>
        /// Full name
        /// </summary>
        [PersonalData]
        public string Name { get; set; }

        /// <summary>
        /// Birth Date
        /// </summary>
        [PersonalData]
        public DateTime DOB { get; set; }
    }

   // 角色表設置主鍵為Int
    public class WebAppIdentityDemoRole : IdentityRole<int>
    {

    }

     (4) 修改上下文

    public class ApplicationDbContext : IdentityDbContext<WebAppIdentityDemoUser, WebAppIdentityDemoRole,int>

    (5) 修改服務注冊

       services.AddIdentity<WebAppIdentityDemoUser, WebAppIdentityDemoRole>()
       //如果使用Identity scaffolder將Identity文件添加到項目中,請刪除對該項目的調用AddDefaultUI
       //.AddDefaultUI()
       .AddEntityFrameworkStores<ApplicationDbContext>()
       .AddDefaultTokenProviders();

    (6) 生成遷移代碼,命令如下

       Add-Migration IdentitySchema

    (7) 同步數據庫

      Update-Database IdentitySchema

    此時表的主鍵類型已修改完成,包括關系表的外鍵類型也同步更新了,如下圖所示:

 

   2.3 添加導航屬性

     導航屬性僅存在於EF模型中,而不存在於數據庫中,如果導航關系沒有改變,模型更改不需要更新數據庫。如果更改關系的模型配置可能比進行其他更改更困難。必須注意取代現有的關系。下面示例是不改變模型關系,只是在user模型上添加導航屬性以及在上下文中指定關系:

    public class WebAppIdentityDemoUser : IdentityUser<int>
    {
        /// <summary>
        /// Full name
        /// </summary>
        [PersonalData]
        public string Name { get; set; }

        /// <summary>
        /// Birth Date
        /// </summary>
        [PersonalData]
        public DateTime DOB { get; set; }

        //定義導航屬性
        public virtual ICollection<IdentityUserClaim<int>> Claims { get; set; }
    }
     protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Core Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Core Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
            builder.Entity<WebAppIdentityDemoUser>(b =>
            {
                // Each User can have many UserClaims
                b.HasMany(e => e.Claims)
                    .WithOne()
                    .HasForeignKey(uc => uc.UserId)
                    .IsRequired();
            });
        }

    對於所有用戶導航屬性, 用戶和角色導航屬性,添加所有導航屬性。參考官網文檔。

 

  2.4  更改表/列名稱,字段長度(上下文中更改)  

     protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            //更改表名稱
            builder.Entity<IdentityUser>(b =>
            {
                b.ToTable("MyUsers");
            });

            //更改表字段名稱
            builder.Entity<IdentityUserClaim<string>>(b =>
            {
                b.Property(e => e.ClaimType).HasColumnName("CType");
                b.Property(e => e.ClaimValue).HasColumnName("CValue");
            });

            //更改長度
            builder.Entity<IdentityUser>(b =>
            {
                b.Property(u => u.UserName).HasMaxLength(128);
            });    
        }

 

   參考文獻

    自定義Identity

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM