Entity Framework Support¶
EntityFrameworkベースの実装は、IdentityServerの構成データと操作上のデータ拡張ポイントに提供されます。EntityFrameworkを使用すると、EFでサポートされているデータベースをこのライブラリで使用できます。
このライブラリのレポはここにあり、NuGetパッケージはここにあります。
このライブラリーが提供する機能は、構成ストアとオペレーティング・ストアの2つの主要な領域に分かれています。これらの2つの異なる領域は、ホスティングアプリケーションのニーズに応じて、単独でまたは一緒に使用できます。
Configuration Store support for Clients, Resources, and CORS settings¶
クライアント、IDリソース、APIリソース、またはCORSデータを、EFでサポートされているデータベース(インメモリ構成を使用するのではなく)からロードする必要がある場合は、構成ストアを使用できます。このサポートは、実装の提供IClientStore、IResourceStoreおよびICorsPolicyService拡張ポイント。これらの実装では、データベース内のテーブルをモデル化するためにDbContext呼び出される継承クラスを使用ConfigurationDbContextします。
構成ストアサポートを使用するにはAddConfigurationStore、次の呼び出しの後に拡張メソッドを使用しますAddIdentityServer。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.EntityFramework-2.0.0;trusted_connection=yes;";
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
// this adds the config data from DB (clients, resources, CORS)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
}
構成ストアを構成するConfigurationStoreOptionsには、構成コールバックに渡されたオプションオブジェクトを使用します。
ConfigurationStoreOptions¶
このオプションクラスには、構成ストアとコンポーネントを制御するプロパティが含まれていますConfigurationDbContext。
ConfigureDbContext- Action<DbContextOptionsBuilder>基底を構成するコールバックとして使用される型のデリゲートConfigurationDbContext。デリゲートはConfigurationDbContext、EFが直接使用されている場合と同じ方法でAddDbContextEFをサポートするデータベースを使用できます。
DefaultSchema- のすべてのテーブルのデフォルトのデータベーススキーマ名を設定できますConfigurationDbContext。
Operational Store support for authorization grants, consents, and tokens (refresh and reference)¶
認可付与、同意、およびトークン(リフレッシュと参照)をEFサポートデータベース(デフォルトのインメモリデータベースではなく)からロードすることが望まれる場合、運用ストアを使用できます。このサポートは、IPersistedGrantStore拡張ポイントの実装を提供します。この実装では、データベース内のテーブルをモデル化するためにDbContext呼び出さPersistedGrantDbContextれた継承クラスを使用します。
操作可能なストアサポートを使用するにはAddOperationalStore、次の呼び出しの後に拡張メソッドを使用しますAddIdentityServer。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.EntityFramework-2.0.0;trusted_connection=yes;";
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30; // interval in seconds
});
}
運用ストアを設定するOperationalStoreOptionsには、設定コールバックに渡されたオプションオブジェクトを使用します。
OperationalStoreOptions¶
このオプションクラスには、操作可能なストアと操作を制御するプロパティが含まれていますPersistedGrantDbContext。
ConfigureDbContext- Delegate of type
Action<DbContextOptionsBuilder>used as a callback to configure the underlyingPersistedGrantDbContext. The delegate can configure thePersistedGrantDbContextin the same way if EF were being used directly withAddDbContext, which allows any EF-supported database to be used. DefaultSchema- Allows setting the default database schema name for all the tables in the
PersistedGrantDbContext. EnableTokenCleanup- 古いエントリがデータベースから自動的にクリーンアップされるかどうかを示します。デフォルトはfalseです。
TokenCleanupInterval- トークンクリーンアップ間隔(秒単位)。デフォルトは3600(1時間)です。
Database creation and schema changes across different versions of IdentityServer¶
異なるバージョンのIdentityServer(およびEFサポート)では、新しく変化する機能に対応するためにデータベーススキーマが変更される可能性が非常に高いです。
私たちはあなたのデータベースを作成したり、あるバージョンから別のバージョンにデータを移行したりするためのサポートを提供していません。組織が適切と考える方法で、データベースの作成、スキーマの変更、およびデータの移行を管理することが求められます。
EFの移行を使用することがこれに可能なアプローチの1つです。移行を使用する場合は、EFクイックスタートでサンプルを入手してください。または、EF移行に関する Microsoftのマニュアルを参照してください。
現バージョンのデータベーススキーマのサンプルSQLスクリプトも公開しています。