使用identity+jwt保护你的webapi(一)——identity基础配置
前言
用戶模塊幾乎是每個系統(tǒng)必備的基礎(chǔ)功能,如果每次開發(fā)一個新項目時都要做個用戶模塊,確實非常無聊。好在asp.net core給我們提供了Identity,使用起來也是比較方便,如果對用戶這塊需求不是非常個性化的話,identity是一個不錯的選擇。
ASP.NET Core Identity:
是一個 API,它支持用戶 登錄功能(UI界面) 。
管理用戶、密碼、配置文件數(shù)據(jù)、角色、聲明、令牌、電子郵件確認等。
Web API中集成Identity
identity是支持UI界面的,如果不是前后端分離項目,可以直接集成identity UI模塊,因為我這里使用Web API,就忽略掉identity UI部分。
安裝相關(guān)包
下面介紹以最小化方式引入identity。
首先創(chuàng)建一個Web API空項目,NuGet安裝identity、efcore、jwt相關(guān)包,數(shù)據(jù)庫我這里就使用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
創(chuàng)建自己的User實體,繼承IdentityUser,IdentityUser中已經(jīng)有一些基礎(chǔ)字段,你可以在你的AppUser中額外定義一些自己需要的字段,比如Address:
public?class?AppUser?:?IdentityUser {[Required]?[StringLength(128)]?public?string?Address?{?get;?set;?} }創(chuàng)建自己的DbContext,繼承IdentityDbContext<>,泛型傳入自己的AppUser:
public?class?AppDbContext?:?IdentityDbContext<AppUser> {public?AppDbContext(DbContextOptions<AppDbContext>?options):?base(options){} }在Startup中配置服務(wù):
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" }這樣一個最簡單的自定義配置就完成了。
數(shù)據(jù)庫遷移
使用dotnet ef命令遷移:
dotnet ef migrations add AppDbContext_Initialdotnet ef database update執(zhí)行完成后已經(jīng)生成了identity相關(guān)表:
修改主鍵類型/表名
identity用戶,角色表的主鍵默認類型是string,默認值是Guid.NewGuid().ToString(),數(shù)據(jù)量不大時無所謂,否則可能存在性能問題。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");?});} }修改完成后更新數(shù)據(jù)庫:
dotnet ef migrations add AppDbContext_Modify_PK_Typedotnet ef database update查看主鍵,表名已成功修改:
最后
本篇完成了identity的基本配置,下一篇將介紹如何使用identity完成用戶注冊登錄,以及獲取jwt token。
參考:
ASP.NET Core 簡介 Identity | Microsoft Docs[1]
Mohamad Lawand - DEV Community[2]
參考資料
[1]
ASP.NET Core 簡介 Identity | Microsoft Docs: https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?view=aspnetcore-5.0&tabs=visual-studio
[2]Mohamad Lawand - DEV Community: https://dev.to/moe23/comments
總結(jié)
以上是生活随笔為你收集整理的使用identity+jwt保护你的webapi(一)——identity基础配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dapr + .NET 实战(六)绑定
- 下一篇: 使用identity+jwt保护你的we