前述の例では、IdeneityServerはメモリキャッシュストレージ方式を使用しており、すべての構成はConfig.csに書き込まれています。実際のアプリケーションでは、データベースストレージ方式を使用して、新しいユーザー、リソース、クライアントの追加など、いつでも構成を容易にする必要があります。また、サーバーのメモリも節約できます。
この記事では、IdentityServer4とMysqlを実装して、クライアントとリソースのデータ、トークンと認証コードのデータ、ユーザーデータの3つの側面からデータベースストレージを実装します。
1.コンテンツを準備する
mysqlデータベース Nuget必須パッケージ IdentityServer4.EntityFramework Pomelo.EntityFrameworkCore.MySql Microsoft.EntityFrameworkCore.Tools データベース接続文字列を追加するための新しいappsettings.jsonファイル { " ConnectionStrings " :{ " SSOConnection ":" server = ipAddress ; userid = root; pwd = Password; database = DB; " } }
第二に、クライアントとリソースのデータベースストレージ
前回の記事では、AddInMemoryメソッドを使用して構成データを読み込み、次にデータベース接続メソッドを使用して構成データを読み込みました。
1.初期化と構築
public IConfiguration Configuration { get ; } public IHostingEnvironment Environment { get ; } パブリックスタートアップ(IConfiguration構成、IHostingEnvironment環境) { 構成 = 構成; 環境 = 環境; }
2.データベース接続を定義し、プロジェクト名を取得します
string connectionString = Configuration.GetConnectionString(" SSOConnection " ); var migrationsAssembly = typeof(Startup).GetTypeInfo()。Assembly.GetName()。Name;
3.コメントAddInMemoryメソッド
AddInMemoryIdentityResources(Config.GetIdentityResources())
AddInMemoryApiResources(Config.GetApis())
AddInMemoryClients(Config.GetClients())
4. AddConfigurationStoreを追加します
.AddConfigurationStore(opt => { opt.ConfigureDbContext = context => { context.UseMySql(connectionString、sql => { sql.MigrationsAssembly(migrationsAssembly); }); }; })
5.クライアントとリソースのデータテーブルを構成し、プロジェクトをスタートアッププロジェクトとして設定し、パッケージマネージャーコンソールを開き、コンソールでデフォルトプロジェクトを設定し、コンソールで次の手順を実行してデータテーブルを追加します。
add-migration ConfigDbContext -c ConfigurationDbContext -o Data / Migrations / IdentityServer / PersistedGrantDb
6.データベースを更新する
データベースを更新する
7. Configsの構成データをデータベースに追加し、Start.csで初期化を実行します
プライベート void InitializeDatabase(IApplicationBuilder app) { using(var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory> ().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext> (); if(!context.Clients.Any()) { foreachの(VARのクライアントでConfigs.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } if(!context.IdentityResources.Any()) { foreachの(VARのリソースでConfigs.GetIdentityResources()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } if(!context.ApiResources.Any()) { foreachの(VARのリソースでConfigs.GetApis()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } } }
3.トークンと認証コードのデータベースストレージ
1. AddOperationalStoreを追加します
.AddOperationalStore(opt => { opt.ConfigureDbContext = context => { context.UseMySql(connectionString、sql => { sql.MigrationsAssembly(migrationsAssembly); }); }; opt.EnableTokenCleanup = true ; opt.TokenCleanupInterval = 30 ; })
2.トークンと認証コードのデータテーブルを構成し、プロジェクトをスタートアッププロジェクトとして設定し、パッケージマネージャーコンソールを開き、コンソールでデフォルトプロジェクトを設定し、コンソールで次の手順を実行してデータテーブルを追加します。
add-migration OperationContext -c PersistedGrantDbContext -o Data / Migrations / IdentityServer / OperationDb
3.データベースを更新する
update-database -c PersistedGrantDbContext