日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

使用identity+jwt保护你的webapi(一)——identity基础配置

發布時間:2023/12/4 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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_Initialdotnet 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_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

總結

以上是生活随笔為你收集整理的使用identity+jwt保护你的webapi(一)——identity基础配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。