【发布时间】:2021-12-26 13:50:42
【问题描述】:
我将 IdentityServer4 (v3.1.x) 与 Entity Framework 包一起使用,以允许将配置和操作设置存储在数据库中。然而,我注意到,出于某种原因,用户登录 IS4 的持续时间由 IIS 应用程序池的“回收间隔”设置决定,我目前将其设置为 30 分钟。这与用户会话的预期行为不同,只要 access_token 持续时间 / refresh_token 持续时间允许。
当 IIS 应用程序池的“回收间隔”设置为更高的值(最大 1740 分钟)时,问题就会消失。然而,我希望“会话”(因为没有更好的名称)在应用程序池和应用程序池回收之间保持持久。
我做错了什么,我需要进行哪些更改才能使其正常工作?
持续时间:
- IdentityTokenLifetime:300s / 5m
- IdentityAccessToken:300s / 5m
- 授权码寿命:300s / 5m
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
string connectionString = _config.GetValue<string>("ConnectionStrings:IdentityServerDBConnectionString");
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddDbContext<IdentityServerDBContext>(x => x.UseSqlServer(connectionString));
var builder = services.AddIdentityServer(
options => {
options.Authentication.CookieLifetime = TimeSpan.FromDays(14); // As test
}
)
.AddSigningCredential(LoadCertificate())
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddProfileService<ProfileService>();
// Custom implementations
services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
services.AddTransient<IProfileService, ProfileService>();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(o =>
{
o.SlidingExpiration = false;
o.ExpireTimeSpan = TimeSpan.FromDays(14); // As test
});
services.AddAuthorization();
}
*编辑: 正如 Tore 在下面的评论中指出的那样,缺少 DataProtection 实现是问题的原因。
使用他分享的链接,我能够通过添加 Microsoft.AspNetCore.DataProtection 包并将以下 sn-p 添加到 Startup 来解决它:
// Persistent data protection
services
.AddDataProtection()
.PersistKeysToDbContext<DBContext>();
【问题讨论】:
标签: c# session identityserver4 persistent