一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情。
为IdentityServer中的配置和操作数据扩展点提供了基于EntityFramework的实现。EntityFramework的使用允许任何EF支持的数据库与Nuget库一起使用
库提供的功能分为两个主要区域:配置存储和操作存储支持。根据托管应用程序的需要,这两个不同的区域可以独立使用或一起使用。
1.配置存储支持客户端,资源和CORS设置
如果希望EF支持的数据库加载客户端,标识资源,API资源或CORS数据(而不是使用内存配置),则可以使用配置存储。此支持提供了IClientStore
和IResourceStore
,以及ICorsPolicyService
可扩展性点的实现。这些实现使用一个DbContext
被调用的类ConfigurationDbContext
来为数据库中的表建模。
要使用配置存储支持,请在调用AddIdentityServer
后使用AddConfigurationStore
扩展方法:
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
的options对象传递给配置回调。
2. ConfigurationStoreOptions
选项类包含用于控制配置存储的属性和ConfigurationDbContext
。
-
ConfigureDbContext
Action<DbContextOptionsBuilder>
用作回调的类型委托来配置底层ConfigurationDbContext
。ConfigurationDbContext
如果直接使用EF,代理可以以相同的方式配置AddDbContext
,这允许使用任何EF支持的数据库。 -
DefaultSchema
允许为ConfigurationDbContext
中的所有表设置默认数据库模式名称。
options.DefaultSchema = "myConfigurationSchema";
如果需要更改迁移历史记录表的架构,可以将另一个操作链接到UserSqlServer
:
options.ConfigureDbContext = b =>
b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly).MigrationsHistoryTable("MyConfigurationMigrationTable", "myConfigurationSchema"));
3. 操作存储支持授权授予,同意和令牌(刷新和引用)
如果希望从EF支持的数据库(而不是默认的内存数据库)加载授权授予,同意和令牌(刷新和引用),则可以使用操作存储。此支持提供了IPersistedGrantStore
可扩展性点的实现。该实现使用一个DbContext
被调用的类PersistedGrantDbContext
来为数据库中的表建模。
要使用操作性存储支持,请在调用AddIdentityServer
后使用AddOperationalStore
扩展方法:
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
的options
对象传递给配置回调。
4. OperationalStoreOptions
此选项类包含用于控制操作PersistedGrantDbContext
存储的属性。
-
ConfigureDbContext
Action<DbContextOptionsBuilder>
用作回调的类型委托来配置底层PersistedGrantDbContext
。PersistedGrantDbContext
如果直接使用EF,代理可以以相同的方式配置AddDbContext
,这允许使用任何EF支持的数据库。 -
DefaultSchema
允许为PersistedGrantDbContext
中的所有表设置默认数据库模式名称。 -
EnableTokenCleanup
指示是否将从数据库中自动清除过时条目。默认是false。 -
TokenCleanupInterval
令牌清理间隔(以秒为单位,默认值为3600 1小时)。
跨不同版本的IdentityServer(以及EF支持)可能会更改数据库架构以适应新的和不断变化的功能,我们不为创建数据库或将数据从一个版本迁移到另一个版本提供任何支持,需要以组织认为合适的任何方式管理数据库创建,架构更改和数据迁移。
5. .NET Identity 支持
提供了基于.NET身份的实现,用于管理IdentityServer用户的身份数据库,实现是IdentityServer中的扩展点,以便为用户加载身份数据以将声明发送到令牌,要使用库,请正常配置ASP.NET Identity。然后在调用AddIdentityServer
后使用AddAspNetIdentity
扩展方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer()
.AddAspNetIdentity<ApplicationUser>();
}
AddAspNetIdentity
需要作为通用参数,为您的用户建模ASP.NET Identity(以及传递给AddIdentityASP.NET Identity 的同一个用户)。这将配置IdentityServer使用实现IUserClaimsPrincipalFactory
,IResourceOwnerPasswordValidator
和IProfileService
的ASP.NET Identity。它还配置了一些用于IdentityServer的ASP.NET Identity选项(例如要使用的声明类型和身份验证cookie设置)。