asp.net core使用identity+jwt保護你的webapi(一)——identity基礎配置
前言
使用者模組幾乎是每個系統必備的基礎功能,如果每次開發一個新專案時都要做個使用者模組,確實非常無聊。好在asp.net core給我們提供了Identity,使用起來也是比較方便,如果對使用者這塊需求不是非常個性化的話,identity是一個不錯的選擇。
ASP.NET Core Identity:
-
是一個 API,它支援使用者 登入功能(UI介面) 。
-
管理使用者、密碼、配置檔案資料、角色、宣告、令牌、電子郵件確認等。
Web API中整合Identity
identity是支援UI介面的,如果不是前後端分離專案,可以直接整合identity UI模組,因為我這裡使用Web API,就忽略掉identity UI部分。
安裝相關包
下面介紹以最小化方式引入identity。
首先建立一個Web API空專案,NuGet安裝identity、efcore、jwt相關包,資料庫我這裡就使用Sqlite:
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.10" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
自定義User,Context
建立自己的User實體,繼承 IdentityUser
, IdentityUser
中已經有一些基礎欄位,你可以在你的 AppUser
中額外定義一些自己需要的欄位,比如 Address
:
public class AppUser : IdentityUser { [Required] [StringLength(128)] public string Address { get; set; } }
建立自己的DbContext,繼承 IdentityDbContext<>
,泛型傳入自己的 AppUser
:
public class AppDbContext : IdentityDbContext<AppUser> { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } }
在Startup中配置服務:
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddDbContext<AppDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentityCore<AppUser>().AddEntityFrameworkStores<AppDbContext>(); }
appsettings.json:
"ConnectionStrings": { "DefaultConnection": "DataSource=app.db; Cache=Shared" }
這樣一個最簡單的自定義配置就完成了。
資料庫遷移
使用 dotnet ef
命令遷移:
dotnet ef migrations add AppDbContext_Initial dotnet ef database update
執行完成後已經生成了identity相關表:
修改主鍵型別/表名
identity使用者,角色表的主鍵預設型別是string,預設值是 Guid.NewGuid().ToString()
,資料量不大時無所謂,否則可能存在效能問題。identity支援主鍵型別的修改;想要修改表名,修改欄位長度等等,也是非常容易:
public class AppUser : IdentityUser<int> { [Required] [StringLength(128)] public string Address { get; set; } }
public class AppDbContext : IdentityDbContext<AppUser, IdentityRole<int>, int> { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<AppUser>(b => { b.ToTable("AppUsers"); }); builder.Entity<IdentityUserClaim<int>>(b => { b.ToTable("AppUserClaims"); }); builder.Entity<IdentityUserLogin<int>>(b => { b.ToTable("AppUserLogins"); }); builder.Entity<IdentityUserToken<int>>(b => { b.ToTable("AppUserTokens"); }); builder.Entity<IdentityRole<int>>(b => { b.ToTable("AppRoles"); }); builder.Entity<IdentityRoleClaim<int>>(b => { b.ToTable("AppRoleClaims"); }); builder.Entity<IdentityUserRole<int>>(b => { b.ToTable("AppUserRoles"); }); } }
修改完成後更新資料庫:
dotnet ef migrations add AppDbContext_Modify_PK_Type dotnet ef database update
檢視主鍵,表名已成功修改:
最後
本篇完成了identity的基本配置,下一篇將介紹如何使用identity完成使用者註冊登入,以及獲取jwt token。
參考:
- 你有沒有覺得郵件傳送人固定配置在yml檔案中是不妥當的呢?SpringBoot 動態設定郵件傳送人
- 力扣 - 劍指 Offer 42. 連續子陣列的最大和
- .Net Core中使用ElasticSearch(二)
- 如何隱藏shell指令碼內容
- JDK中Lambda表示式的序列化與SerializedLambda的巧妙使用
- mybatis一對多查詢resultMap只返回了一條記錄
- 力扣 - 劍指 Offer 10- I. 斐波那契數列
- SpringCloud升級之路2020.0.x版-42.SpringCloudGateway 現有的可供分析的請求日誌以及缺陷
- 正則表示式學習 --- 02
- Go語言核心36講(Go語言實戰與應用十四)--學習筆記
- 從一個小Bug,到Azure DevOps
- gantt甘特圖可拖拽、編輯(vue、react都可用 highcharts)
- 『學了就忘』Linux使用者管理 — 51、使用者管理相關命令
- SpringCloud升級之路2020.0.x版-41. SpringCloudGateway 基本流程講解(3)
- Go語言核心36講(Go語言實戰與應用十三)--學習筆記
- 痞子衡嵌入式:嵌入式Cortex-M系統中斷延遲及其測量方法簡介
- 論發展的十大關係。總結過去,正視未來!!!
- 【Azure 應用服務】App Service 無法連線到Azure MySQL服務,報錯:com.mysql.cj.jdbc.exceptions.C...
- 前端:WebP自適應實踐
- Python學習手冊——第二部分 型別和運算(1)之字串