• 第62章 EntityFramework支持


    为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地址

  • 相关阅读:
    Go语言之深入剖析slice
    docker-compose 安装以及遇到的错误解决
    linux 简单记录9 --服务的访问控制列表(ssh,scp,screen)
    linux 简单记录8 --iptables 与 firewalld 防火墙
    linux 简单记录7--使用 RAID 与 LVM 磁盘阵列技术(使用LVM扩展磁盘空间)
    linux 简单记录6--存储结构与磁盘划分
    DNS 域名解析服务
    linux运用软链接解决目录空间不足
    linux 简单记录5--用户身份与文件权限
    ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper
  • 原文地址:https://www.cnblogs.com/thinksjay/p/10787402.html
Copyright © 2020-2023  润新知