.NET Core IdentityServer4實戰 第三章-使用EntityFramework Core進行持久化配置
內容:本文帶大家使用IdentityServer4進行使用使用EntityFramework Core進行配置和操作資料
作者:zara(張子浩) 歡迎分享,但需在文章鮮明處留下原文地址。
前兩章內容呢,無論是Client定義還是Server端物件資源定義都是儲存與記憶體,當然這個問題也被博友問到,那麼我們如何從資料庫中讀取呢,當然這個IdentityServre已經想好為我們進行處理了,那麼只需要安裝 IdentityServer4.EntityFramework 就可以了。
一般來說都會使用EF做遷移,那麼如果讓EF支援.NET Cli命令列呢,開啟專案。
在 </project> 之前新增一下程式碼,用於支援Cli命令列,再此之後你可以去專案根目錄通過cmd.exe 執行 doenet ef,如果沒有出現error,就ok了!如果出現問題大多是都是這個放錯位置了。
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
鑑於EF的靈活性,我們本篇文章使用SqlServer資料庫,就現在我們建立一個數據庫,還有一些相關的表。
為 IdentityServer4.EntityFramework.Storage中 的實體維護SqlServer的最新SQL指令碼 。 他們就 在這裡 。(隨便建立一個數據庫,把那兩個表放進去執行就好)
下面我們要在Server進行配置了,需要在 Startup.cs 中的方法 ConfigureServices 進行修改,首先我們定義了一個常量 connectionString ,這你肯定知道,這是一個數據庫連線字串,再往下定義了一個 migrationsAssembly ,它獲取了程式集的名稱,再通過 AddIdentityServer 以及 AddOperationalStore 方法對資料庫的相關遷移配置進行了賦值。
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-2.0.0;trusted_connection=yes;"; var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; // configure identity server with in-memory stores, keys, clients and scopes services.AddIdentityServer() .AddTestUsers(Config.GetUsers()) // this adds the config data from DB (clients, resources) .AddConfigurationStore(options => { options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)); }) // this adds the operational data from DB (codes, tokens, consents) .AddOperationalStore(options => { options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)); // this enables automatic token cleanup. this is optional. options.EnableTokenCleanup = true; });
再此期間你會引用 System.Reflection 以及 Microsoft.EntityFrameworkCore ,如上面所述,上面是硬編碼形式進行配置了,更確切的說這些關於資料庫的配置都是一個叫做 ConfigurationStoreOptions 的物件,你可以隨便修改它,EF相容的的資料庫它都是可以的。因為IdentityServer4實現了EF可實現的介面。
它從哪裡實現了呢?我們不難發現,在 IdentityServerEntityFrameworkBuilderExtensions 中的定義中有一個叫做 AddOperationalStore 的引數,它就是為了新增動態的儲存庫,定義如下。
public static IIdentityServerBuilder AddOperationalStore<TContext>(this IIdentityServerBuilder builder, Action<OperationalStoreOptions> storeOptionsAction = null) where TContext : DbContext, IPersistedGrantDbContext;
可見它實現了DbContext,而大家都是.NET 程式設計師,所以我覺得你應該知道了怎麼回事了,就現在,請確保你的連線字串正確,我們要開始進行遷移了!執行以下命令.
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
如果沒有什麼問題,那麼結果一定如下所示,當然您的專案中一定要安裝 Microsoft.EntityFrameworkCore ,否則將會出現 ERROR:Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding .
您現在應該在專案中看到一個 〜/Data/Migrations/IdentityServer 資料夾。 這包含新建立的遷移的程式碼。現在我們已經添加了遷移,我們可以編寫程式碼來從遷移中建立資料庫。 我們還將使用我們在之前的快速入門中定義的記憶體配置資料對資料庫進行種子處理。在Startup.cs中新增這個方法來幫助初始化資料庫:
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.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } if (!context.IdentityResources.Any()) { foreach (var resource in Config.GetIdentityResources()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } if (!context.ApiResources.Any()) { foreach (var resource in Config.GetApis()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } } }
上面的程式碼可能需要將這些名稱空間新增到您的檔案中:
using IdentityServer4.EntityFramework.DbContexts; using IdentityServer4.EntityFramework.Mappers;
然後我們可以從 Configure
方法中 呼叫它 :
public void Configure(IApplicationBuilder app) { InitializeDatabase(app); }
現在,如果執行IdentityServer專案,則應建立資料庫並使用快速入門配置資料進行種子設定。 您應該能夠使用SQL Server Management Studio或Visual Studio來連線和檢查資料。
最後你可以根據這個Client表來配置你的Config.cs中的GetUsers這樣服務端的配置也就如此了,當然你可以自定義資料庫的欄位來適應你的應用程式,那麼當然沒更新一次你就可以通過EF的相關命令倆更新資料庫。那麼所有的更新記錄就在 __EFMigrationsHistory 表中。