IdentityServer4在30分钟后自动注销

问题描述 投票:0回答:1

我有带有Angular的IdentityServer4。每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();
    }

@ EDIT

如果我要添加

services.Configure<SecurityStampValidatorOptions>(options =>
{
    options.ValidationInterval = TimeSpan.FromHours(24);
});

然后可以正常工作,但是我敢打赌这不是解决我问题的正确方法。


@ EDIT2

我发现了这个https://github.com/IdentityModel/oidc-client-js/issues/911#issuecomment-617724445这对我有所帮助,但仍然不确定是否是解决此问题的正确方法,还是只是下次破解。

c# cookies .net-core authorization identityserver4
1个回答
0
投票

请看http://docs.identityserver.io/en/stable/topics/signin.html#overriding-cookie-handler-configuration

在完成.AddAuthentication的操作后,您丢失了自己的AddIdentityServer。类似于:

services.AddIdentityServer()
...
services.AddAuthentication("MyCookie")
.AddCookie("MyCookie", options =>
{
    options.ExpireTimeSpan = ...;
});

使用IdentityServerConstants.DefaultCookieAuthenticationScheme而不是MyCookie,您将覆盖身份的默认身份验证cookie配置。

© www.soinside.com 2019 - 2024. All rights reserved.