IdentityServer4(八)使用EntityFramework Core对数据进行持久化
上幾篇,我們創(chuàng)建了客戶端,scope,啟動時,IdentityServer把這些配置數(shù)據(jù)加載至內(nèi)存,但是,如果我們想要更改配置,就必須停掉IdentityServer,然后重新啟動。且IdentityServe在r運行過程中還會生成臨時數(shù)據(jù),如授權(quán)碼、是否同意的按鈕選擇、以及refresh token。默認(rèn)情況下,這些也存儲在內(nèi)存中。
將以上這些數(shù)據(jù)存儲在數(shù)據(jù)庫中進行數(shù)據(jù)持久化,方便重啟跨多個IdentityServer實例,這個持久化,我們可以使用IdentityServer4 Entity Framework
除了手動配置EF支持之外,還有一個IdentityServer模板可以使用,dotnet new is4ef創(chuàng)建一個支持EF的新項目。
IdentityServer4.EntityFrameworknuget包實現(xiàn)了所需的存儲和服務(wù),主要使用以下兩個DbContexts:
ConfigurationDbContext - 作用于注冊數(shù)據(jù),如客戶端,資源,scope等等
PersistedGrantDbContext - 作用于臨時操作數(shù)據(jù),如授權(quán)碼,refresh tokens
這些context適用于任何ef core兼容的關(guān)系型數(shù)據(jù)庫,sqlserver,mysql。
可以在
IdentityServer4.EntityFramework.Storage包中找到context,entities,IdentityServer4 stores
IdentityServer4.EntityFramework包括了注冊的擴展方法,且包括了IdentityServer4.EntityFramework.Storage
1.添加nuget引用
cd .\IdentityServer\ dotnet add package IdentityServer4.EntityFramework2.添加對mysql的支持
dotnet add package MySql.Data.EntityFrameworkCore3.數(shù)據(jù)遷移
IdentityServer4.EntityFramework.Storage包存在包含映射自IdentityServer模型的實體類,隨著IdentityServer的模型的更改,IdentityServer4.EntityFramework.Storage中的實體類也將更改,所以需要使用者隨著時間的推移,升級使用這個包,這個過程,需要負(fù)責(zé)在數(shù)據(jù)庫架構(gòu)以及在實體類更改時,對該數(shù)據(jù)庫架構(gòu)進行必要的更改。最好的方式就是使用EF數(shù)據(jù)遷移(EF migrations)
這里官方只提供了針對sqlserver的sql腳本,可以看一下,做個了解。
4.重新配置存儲
在Startup.cs
using?Microsoft.EntityFrameworkCore; using?System.Reflection;?//這里用到了反射var?migrationsAssembly?=?typeof(Startup).GetTypeInfo().Assembly.GetName().Name;//3308為宿主機端口,映射docker?mysql容器默認(rèn)端口3306 const?string?connectionString?=?@"Persist?Security?Info=False;database=IdentityServer4;server=localhost;port=3308;Connect?Timeout=30;user?id=root;?pwd=123456";services.AddIdentityServer().AddTestUsers(TestUsers.Users).AddConfigurationStore(options?=>{options.ConfigureDbContext?=?b?=>?b.UseMySQL(connectionString,sql?=>?sql.MigrationsAssembly(migrationsAssembly));}).AddOperationalStore(options?=>{options.ConfigureDbContext?=?b?=>?b.UseMySQL(connectionString,sql?=>?sql.MigrationsAssembly(migrationsAssembly));});因為我們在IdentityServer.csproj中使用EF遷移,所以通過對MigrationsAssembly的調(diào)用來告訴Entity Framework 的宿主項目(IdentityServer.csproj)將包含遷移代碼(the migrations code)。這是必要的,因為宿主項目(IdentityServer.csproj)與包含DbContext類的項目,兩者是位于不同的程序集中(IdentityServer4.EntityFramework.Storage)。
5.創(chuàng)建遷移
一旦將IdentityServer配置為使用 Entity Framework Core,我們將需要生成一些遷移-migrations。
Entity Framework Core CLI
Microsoft.EntityFrameworkCore.Design nuget包
#安裝ef core 工具 dotnet tool install --global dotnet-ef dotnet add package Microsoft.EntityFrameworkCore.Design#cd到IdentityServer項目目錄 dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDbdotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb溫故而知新:還記得在VS的Package Manager Console是如何執(zhí)行命令創(chuàng)建遷移的嗎?
#第一步Add-Migration InitialCreate#第二步Update-Database6.初始化數(shù)據(jù)庫
現(xiàn)在我們已經(jīng)完成了遷移,我們可以編寫代碼從遷移-migrations創(chuàng)建數(shù)據(jù)庫。我們還可以使用在前面的quickstart中定義的內(nèi)存配置數(shù)據(jù)來為數(shù)據(jù)庫初始化種子,當(dāng)然這個seed最好只是在調(diào)試環(huán)境下執(zhí)行。
官方提示:在這個快速入門中使用的方法主要是使IdentityServer更容易啟動和運行。您應(yīng)該設(shè)計適合自己體系結(jié)構(gòu)的數(shù)據(jù)庫創(chuàng)建和維護策略。
在Startup.cs中增加下面的初始化方法:
using?System.Linq; using?IdentityServer4.EntityFramework.DbContexts; using?IdentityServer4.EntityFramework.Mappers;private?void?InitializeDatabase(IApplicationBuilder?app) {using?(var?serviceScope?=?app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()){serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();var?context?=?serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();context.Database.Migrate();if?(!context.Clients.Any()){foreach?(var?client?in?Config.Clients){context.Clients.Add(client.ToEntity());}context.SaveChanges();}if?(!context.IdentityResources.Any()){foreach?(var?resource?in?Config.IdentityResources){context.IdentityResources.Add(resource.ToEntity());}context.SaveChanges();}if?(!context.ApiResources.Any()){foreach?(var?resource?in?Config.Apis){context.ApiResources.Add(resource.ToEntity());}context.SaveChanges();}} }public?void?Configure(IApplicationBuilder?app) {//?this?will?do?the?initial?DB?populationInitializeDatabase(app);//?the?rest?of?the?code?that?was?already?here//?... }上面的InitializeDatabase方法可以方便地 seed the database,但是這種方法在每次運行應(yīng)用程序時都留進去執(zhí)行并不理想。一旦填充數(shù)據(jù)庫初始化數(shù)據(jù)之后,就可以考慮刪除對其之調(diào)用。
7.運行客戶端應(yīng)用
這個就簡略些,上個命令即可
cd src\IdentityServer dotnet run 長按二維碼關(guān)注點外賣,先領(lǐng)券總結(jié)
以上是生活随笔為你收集整理的IdentityServer4(八)使用EntityFramework Core对数据进行持久化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Halcon和Opencv的区别?
- 下一篇: 限制IIS站点的内存,避免级联影响