可以将 .AddOperationalStore 与 .InMemoryResources/Cients 一起用于 PersistedGrants 吗?

Its is Possible to use .AddOperationalStore for PersistedGrants with .InMemoryResources/Cients?

我想将 PersistedGrants 存储在数据库中以用于 Azure Scale Out。

我更喜欢从 appsettings.json 而不是从数据库中读取 IdentityResources/ApiResources/Clients(Config)。

How To on IdentityServer4's doc site 中显示了一个将两者持久保存到数据库的示例。

是否可以在存储 Grants 时不将配置数据加载到数据库中?如果有怎么办?

这个有效:

        services.AddIdentityServer((options) =>
        {
            options.UserInteraction = new UserInteractionOptions
            {
                LoginUrl = "/login",
                LogoutUrl = "/api/Account/Logout",
                ErrorUrl = "/error"
            };
        })
       .AddSigningCredential(X509CertificateHelper.getCert(_env))            
       .AddOperationalStore(builder => builder.UseSqlServer(connectionString,
           options => options.MigrationsAssembly(migrationsAssembly)))           
       .AddConfigurationStore(builder => builder.UseSqlServer(connectionString,
           options => options.MigrationsAssembly(migrationsAssembly)))
       .AddAspNetIdentity<ApplicationUser>();

这不是。具体来说我不能 运行 dotnet ef database update -c PersistedGrantDbContext.

        services.AddIdentityServer((options) =>
        {
            options.UserInteraction = new UserInteractionOptions
            {
                LoginUrl = "/login",
                LogoutUrl = "/api/Account/Logout",
                ErrorUrl = "/error"
            };
        })
        .AddSigningCredential(X509CertificateHelper.getCert(_env))           
        .AddOperationalStore(builder => builder.UseSqlServer(connectionString,
            options => options.MigrationsAssembly(migrationsAssembly)))
        .AddInMemoryIdentityResources(Config.GetIdentityResources())
        .AddInMemoryApiResources(Config.GetApiResources())
        .AddInMemoryClients(Config.GetClients(AppSettingsHelper.GetIdSrvrSettings()))
        .AddAspNetIdentity<ApplicationUser>();

对于后者,我得到以下错误:

An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Cannot perform runtime binding on a null reference

"Is it possible to not load configuration data into Database while storing Grants? If so how?"

是的。您正在使用 EntityFrame 工作来实现持久性,因此我提供的信息可能不完全兼容……我们使用其他方法来管理数据库持久性。

我们的 ConfigureServices 看起来像这样:

services.AddIdentityServer(x =>
            {
                x.IssuerUri = webServerSettings.Host;
            })
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddInMemoryIdentityResources(Config.GetIdentityResources());

services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();

通过这种方式,我们的 IdentityResourcesApiResourcesClients 是从配置文件中提取的,而我们的 Grants 是通过我们自己实现的 IPersistedGrantStore。我们没有对该接口做任何特别的事情:它是一个非常直接的接口,只需几个简单的方法即可实现。