IdentityServer4 在 30 分钟后自动注销
IdentityServer4 automatically logout after 30 minutes
我有 IdentityServer4 和 Angular。令牌每 5 分钟静默刷新一次。但 30 分钟后,用户将自动注销。我试图以某种方式设置终身 cookie,但没有成功。
这是我当前的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Identity")));
services.AddIdentity<AppUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.SignIn.RequireConfirmedEmail = true;
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = null;
})
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer(options => options.Authentication.CookieLifetime = TimeSpan.FromHours(10))
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients(Configuration["AppUrls:ClientUrl"]))
.AddAspNetIdentity<AppUser>();
services.AddTransient<IProfileService, IdentityClaimsProfileService>();
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddRazorPages().AddRazorRuntimeCompilation();
}
@编辑
如果我会加
services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.FromHours(24);
});
然后它工作正常,但我敢打赌这不是我的问题的正确解决方案。
@EDIT2
我找到了这个
https://github.com/IdentityModel/oidc-client-js/issues/911#issuecomment-617724445
这对我有帮助,但仍然不确定是解决它的正确方法还是下一个 hack。
据我所知,这既不是 Identity Server 4 也不是 OpenID Connect 问题。
这是Asp.Net身份cookie的逻辑。这应该可以在 Startup.cs.
配置
您需要添加以下cookie配置:
services.ConfigureApplicationCookie(o =>
{
o.ExpireTimeSpan = TimeSpan.FromHours(24);
o.SlidingExpiration = true;
});
此答案的灵感来自于以下答案:
- ASP.NET Identity Session Timeout
- Why does my IdentityServer4 based server timeout in 30 minutes and only support SSO in the first 30 minutes?
我找到了解决方案。
我正在使用
await HttpContext.SignInAsync(user.Id, user.UserName, props);
用于登录用户。并且它导致了问题。
修改后:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure: true);
它开始正常工作。
我有 IdentityServer4 和 Angular。令牌每 5 分钟静默刷新一次。但 30 分钟后,用户将自动注销。我试图以某种方式设置终身 cookie,但没有成功。
这是我当前的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Identity")));
services.AddIdentity<AppUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.SignIn.RequireConfirmedEmail = true;
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = null;
})
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer(options => options.Authentication.CookieLifetime = TimeSpan.FromHours(10))
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients(Configuration["AppUrls:ClientUrl"]))
.AddAspNetIdentity<AppUser>();
services.AddTransient<IProfileService, IdentityClaimsProfileService>();
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddRazorPages().AddRazorRuntimeCompilation();
}
@编辑
如果我会加
services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.FromHours(24);
});
然后它工作正常,但我敢打赌这不是我的问题的正确解决方案。
@EDIT2
我找到了这个 https://github.com/IdentityModel/oidc-client-js/issues/911#issuecomment-617724445 这对我有帮助,但仍然不确定是解决它的正确方法还是下一个 hack。
据我所知,这既不是 Identity Server 4 也不是 OpenID Connect 问题。
这是Asp.Net身份cookie的逻辑。这应该可以在 Startup.cs.
配置您需要添加以下cookie配置:
services.ConfigureApplicationCookie(o =>
{
o.ExpireTimeSpan = TimeSpan.FromHours(24);
o.SlidingExpiration = true;
});
此答案的灵感来自于以下答案:
- ASP.NET Identity Session Timeout
- Why does my IdentityServer4 based server timeout in 30 minutes and only support SSO in the first 30 minutes?
我找到了解决方案。 我正在使用
await HttpContext.SignInAsync(user.Id, user.UserName, props);
用于登录用户。并且它导致了问题。
修改后:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure: true);
它开始正常工作。