我將 IdentityServer4 (v3.1.x) 與物體框架包一起使用,以允許將配置和操作設定存盤在資料庫中。但是我注意到,出于某種原因,用戶登錄到 IS4 的持續時間由 IIS 應用程式池的“回收間隔”設定決定,我目前將其設定為 30 分鐘。這與預期的行為不同,用戶的會話只要 access_token 持續時間/ refresh_token 持續時間允許。
當 IIS 應用程式池的“回收間隔”設定為更高的值(最多 1740 分鐘)時,問題就會消失。然而,我期望“會話”(因為沒有更好的名稱)在應用程式池和應用程式池回收之間保持持久。
我做錯了什么,我需要改變什么才能使它作業?
持續時間:
- IdentityTokenLifetime:300s / 5m
- IdentityAccessToken:300s / 5m
- AuthorizationCodeLifetime: 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 包并將以下代碼段添加到 Startup 來解決它:
// Persistent data protection
services
.AddDataProtection()
.PersistKeysToDbContext<DBContext>();
uj5u.com熱心網友回復:
cookie 是使用 ASP.NET Core 中的資料保護 API 加密的,對于生產來說,正確配置它是明智的,因此加密密鑰/密鑰環會保留在您的 Web 應用程式之外。
我在這里寫了關于資料保護 API 的博客:
- 將 ASP.NET Core 資料保護密鑰環存盤在 Azure Key Vault 中
另請參閱這些鏈接:
- 開始使用 ASP.NET Core 中的資料保護 API
- ASP.NET Core 中的資料保護配置
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359072.html
上一篇:在其他站點(mercadopago.com)完成付款后,PHP會話被銷毀
下一篇:在多個子域之間共享會話
