第62章 EntityFramework支持 - Identity Server 4 中文文档(v1.0.0)

为IdentityServer中的配置和操作数据扩展点提供了基于EntityFramework的实现。EntityFramework的使用允许任何EF支持的数据库与此库一起使用。

这个库的仓库位于这里,NuGet包就在这里

此库提供的功能分为两个主要区域:配置存储和操作存储支持。根据托管应用程序的需要,这两个不同的区域可以独立使用或一起使用。

62.1 配置存储支持客户端,资源和CORS设置

如果希望从EF支持的数据库加载客户端,标识资源,API资源或CORS数据(而不是使用内存配置),则可以使用配置存储。此支持提供了IClientStoreIResourceStore,以及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对象传递给配置回调。

62.2 ConfigurationStoreOptions

此选项类包含用于控制配置存储的属性和ConfigurationDbContext

  • ConfigureDbContext
    Action<DbContextOptionsBuilder>用作回调的类型委托来配置底层ConfigurationDbContextConfigurationDbContext如果直接使用EF,代理可以以相同的方式配置AddDbContext,这允许使用任何EF支持的数据库。

  • DefaultSchema
    允许为ConfigurationDbContext中的所有表设置默认数据库模式名称。

options.DefaultSchema = "myConfigurationSchema";

如果需要更改迁移历史记录表的架构,可以将另一个操作链接到UserSqlServer

options.ConfigureDbContext = b =>
    b.UseSqlServer(connectionString,
        sql => sql.MigrationsAssembly(migrationsAssembly).MigrationsHistoryTable("MyConfigurationMigrationTable", "myConfigurationSchema"));

62.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
        });
}

要配置操作存储,请使用OperationalStoreOptionsoptions对象传递给配置回调。

62.4 OperationalStoreOptions

此选项类包含用于控制操作PersistedGrantDbContext存储的属性。

  • ConfigureDbContext
    Action<DbContextOptionsBuilder>用作回调的类型委托来配置底层PersistedGrantDbContextPersistedGrantDbContext如果直接使用EF,代理可以以相同的方式配置AddDbContext,这允许使用任何EF支持的数据库。

  • DefaultSchema
    允许为PersistedGrantDbContext中的所有表设置默认数据库模式名称。

  • EnableTokenCleanup
    指示是否将从数据库中自动清除过时条目。默认是false。

  • TokenCleanupInterval
    令牌清理间隔(以秒为单位)。默认值为3600(1小时)。

62.5 跨不同版本的IdentityServer的数据库创建和模式更改

跨不同版本的IdentityServer(以及EF支持)很可能会更改数据库架构以适应新的和不断变化的功能。

我们不为创建数据库或将数据从一个版本迁移到另一个版本提供任何支持。您需要以组织认为合适的任何方式管理数据库创建,架构更改和数据迁移。

使用EF迁移是一种可行的方法。如果您确实希望使用迁移,请参阅EF快速入门以获取有关如何入门的示例,或参阅有关EF迁移的Microsoft文档

我们还为当前版本的数据库模式发布了示例SQL脚本

github地址

转载于:https://www.cnblogs.com/thinksjay/p/10787402.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值