根据 StartUp.cs 设置,Cookie 不会过期

Cookie not expiring per StartUp.cs settings

我在 2 天的时间里尝试了很多解决方案,但仍然无法正常工作。我想要的是用户 cookie 在一定时间后过期

例如用户 A 登录并转到主页,用户 A 去午休。用户 A 返回并单击导航栏并被重定向到登录页面。

我已经尝试了 AddAuthentication()AddSession()AddCookie() 选项中的所有选项,所有选项都有我选择的 ExpireTimeSpanCookie.Expiration。似乎没有任何效果。该项目使用 ASP.NET Identity 并且我知道应该在 cookie 选项之前调用此服务。请在下面查看我当前的 StartUp.cs,这是我尝试的最后一件事:

Startup.cs

public class Startup
    {
        public IConfiguration Configuration { get; }
        public IContainer ApplicationContainer { get; private set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddIdentity<ApplicationUser, IdentityRole>(config =>
                {
                    config.SignIn.RequireConfirmedEmail = true;
                })
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            //other services e.g. interfaces etc.
            services.AddAuthentication().AddCookie(options =>
            {
                options.Cookie.HttpOnly = true;
                options.Cookie.Expiration = TimeSpan.FromSeconds(60);
                options.LoginPath = "/Account/Login";
                options.LogoutPath = "/Account/Logout";
                options.AccessDeniedPath = "/AccessDenied";
                options.ExpireTimeSpan = TimeSpan.FromSeconds(5);
                options.SlidingExpiration = true;
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            //services.AddSession();

            var containerBuilder = new ContainerBuilder();
            containerBuilder.Populate(services);
            this.ApplicationContainer = containerBuilder.Build();
            var serviceProvider = new AutofacServiceProvider(this.ApplicationContainer);

            return serviceProvider;
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.ConfigureCustomExceptionMiddleware();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            //app.UseSession();
            app.UseMvc();
        }
    }

以下代码不影响身份 cookie:

services.AddAuthentication().AddCookie(options => ...);

相反,它添加了一个名为 Cookies 的新 cookie-based 身份验证方案,并配置了 that。对于所有标准身份设置,此方案未被使用,因此对其配置的任何更改都将无效。

Identity 使用的主要身份验证方案名为 Identity.Application,并在您的示例中的 AddIdentity<TUser, TRole> 方法中注册。这可以使用 ConfigureApplicationCookie 进行配置。这是一个例子:

services.ConfigureApplicationCookie(options => ...);

有了它,cookie 选项将按预期受到影响,但为了设置具有 non-session 生命周期的 cookie,您还需要将 isPersistent 设置为 true 在你对 PasswordSignInAsync 的调用中。这是一个例子:

await signInManager.PasswordSignInAsync(
    someUser, somePassword, isPersistent: true, lockoutOnFailure: someBool);