【问题标题】:IdentityServer4 automatically logout after 30 minutesIdentityServer4 30 分钟后自动注销
【发布时间】:2020-09-11 22:55:18
【问题描述】:

我有带有 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();
    }

@编辑

如果我要添加

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

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


@EDIT2

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

【问题讨论】:

  • 您是否尝试将options.Authentication.CookieSlidingExpiration 设置为true?我在your code 没有看到它,这就是为什么要问。在 IdentityServer4 代码中,cookie 属性设置为 here。如果这些值不起作用,那么肯定不是 IdentityServer 问题

标签: c# cookies .net-core authorization identityserver4


【解决方案1】:

据我所知,这既不是 Identity Server 4 也不是 OpenID Connect 问题。

这是 Asp.Net Identity cookie 的逻辑。这应该可以在 Startup.cs 中配置。

您需要添加以下cookie配置:

services.ConfigureApplicationCookie(o =>
{
    o.ExpireTimeSpan = TimeSpan.FromHours(24);
    o.SlidingExpiration = true;
});

这个答案的灵感来自以下答案:

【讨论】:

  • 没有帮助。你能看看我的代码库吗? github.com/pklejnowski/angular_core
  • @DiPix 我今天下午完成我的工作后会检查一下 :)
  • 呀,还没说完就有点魔鬼圈了。希望会成功。有一些事情发生时会尽快通知你
  • 我认为我现在不能带来更多的东西,我已经花了很多时间在上面。我花了几个小时得到解决方案,这应该很容易,但遇到了不同的问题。当它最终运行时,我开始调试超时问题,到目前为止没有运气。不管怎样,这个问题让我想解决它。这是时间问题。
  • 猜猜大家都放弃了:(
【解决方案2】:

我找到了解决方案。 我正在使用

await HttpContext.SignInAsync(user.Id, user.UserName, props);

用于登录用户。这就是问题的根源。

改成之后:

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure: true);

它开始正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2017-06-27
    • 2011-08-04
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2018-02-05
    相关资源
    最近更新 更多