【问题标题】:Session logged out too soon会话过早退出
【发布时间】:2018-11-23 17:28:57
【问题描述】:

我正在使用带有 Microsoft Identity 的 ASP.NET Core 2.1,并且用户抱怨他们在闲置大约 30 分钟后不断被重定向到登录屏幕。我在 ExpireTimeSpan 中设置了 60 分钟,但它从来没有持续这么长时间。有什么建议吗?

这就是我在 Startup.cs 文件中的内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IRFDbRepository, RFDbRepository>();
    var connection = _configuration.GetConnectionString("RFDbConnection");
    services.Configure<ConnectionStrings>(_configuration.GetSection("ConnectionStrings"));
    services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(connection));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddRazorPagesOptions(options =>
    {
        options.AllowAreas = true;
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

    services.AddIdentity<User, UserRole>().AddDefaultTokenProviders();
    services.AddTransient<IUserStore<User>, UserStore>();
    services.AddTransient<IRoleStore<UserRole>, RoleStore>();

    services.ConfigureApplicationCookie(options =>
    {
        options.LoginPath = "/Identity/Account/Login";
        options.LogoutPath = "/Identity/Account/Logout";
        options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
        options.SlidingExpiration = true;
    });
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRFDbRepository rFDbRepository)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    loggerFactory.AddFile(_configuration.GetValue<string>("Logging:LogFile"));
    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
        routes.MapRoute(
            name: "ActionApi",
            template: "api/{controller}/{action}/{id?}");
    });
}

【问题讨论】:

  • 尝试在var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);中将RememberMe设置为True
  • 现在试过了,恐怕没有什么不同。还是谢谢

标签: c# asp.net-core asp.net-identity


【解决方案1】:

我终于找到了这个问题的根源。

ASP.NET Core 2.1 中的 Identity 存在一个问题,如果您实现了自己的 UserStore 版本但未实现 IUserSecurityStampStore,则将跳过大多数与安全标记相关的功能。

当您调用 AddIdentity() 时,它每 30 分钟对 securityStamp 进行一次验证检查。

这会导致用户在 30 分钟后退出的令人困惑的行为,即使 cookie 没有过期。

显然,ASP.NET Core 2.2 中对此进行了修复,此处提供更多详细信息

https://github.com/aspnet/Identity/issues/1880

与此同时,您可以让您的 UserStore 实现 IUserSecurityStampStore,或者执行我现在做的快速修复,将其添加到您的 startup.cs 中,从而将故障之间的时间从 30 分钟增加到 10 小时。

services.Configure(o => o.ValidationInterval = TimeSpan.FromHours(10));

【讨论】:

    【解决方案2】:

    上一个答案只是将问题从每 30 分钟发生一次更改为每 10 小时发生一次,这并不理想。

    在我的情况下,我根本没有将这种类型的用户存储在数据库中,所以当然无法检查安全标记 - 所以在我的情况下,我只是像这样禁用了该验证:

                services.ConfigureApplicationCookie(opt =>
                {
                    opt.Events.OnValidatePrincipal = (ctx) =>
                    {
                        // Do not validate principal (i.e. security stamp) for our custom users
                        // the default behavior will log them out every SecurityStampValidatorOptions.ValidationInterval
                        if (ctx.Principal.HasClaim(x => x.Type == CmsClaimTypes.ClaimType)) return Task.CompletedTask;
                        return SecurityStampValidator.ValidatePrincipalAsync(ctx);
                    };
                });
    

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2016-04-26
      • 2013-07-03
      • 2015-09-14
      相关资源
      最近更新 更多