asp.net-core - 无法在 ASP.NET Core 2.0 中增加 session 超时

标签 asp.net-core asp.net-core-2.0

我无法在 ASP.NET Core 2.0 中增加 session 超时。 session 每 20 到 30 分钟就会过期。当我将时间减少到 1 分钟并进行调试时,它工作正常,但是当它增加到超过 30 分钟或数小时/天时,它不会持续到指定的持续时间。
session 在 Debug模式以及来自 IIS(发布后托管)下 30 分钟后过期。

options.IdleTimeout = TimeSpan.FromMinutes(180);  
我在 startup.cs 文件中使用以下代码。
public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        
        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = true;
            options.Cookie.Expiration = TimeSpan.FromDays(3);
            options.ExpireTimeSpan= TimeSpan.FromDays(3);
            options.SlidingExpiration = true;
        });

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromMinutes(180);                
        });
        services.AddSingleton<IConfiguration>(Configuration);
        
       
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IConfigurationSettings configurationSettings)
    {
       
        //
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseSession();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseExceptionHandler(
         builder =>
         {
             builder.Run(
             async context =>
             {
                 context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                 context.Response.ContentType = "application/json";
                 var ex = context.Features.Get<IExceptionHandlerFeature>();
                 if (ex != null)
                 {                         
                     if(ex.Error is SessionTimeOutException)
                     {
                         context.Response.StatusCode = 333;
                     }
                     if (ex.Error is UnauthorizedAccessException)
                     {
                         context.Response.StatusCode =999;
                     }
                     var err = JsonConvert.SerializeObject(new Error()
                     {
                         Stacktrace = ex.Error.StackTrace,
                         Message = ex.Error.Message
                     });
                     await context.Response.Body.WriteAsync(Encoding.ASCII.GetBytes(err), 0, err.Length).ConfigureAwait(false);
                 }
             });
         });

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

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });
    }

最佳答案

问题可能会出现,因为您的应用程序池在 IIS 上被回收。默认情况下,IIS 应用程序池每 20 分钟后回收一次。

尝试增加应用程序池回收时间。

更改应用程序池回收时间。通过以下链接

https://www.coreblox.com/blog/2014/12/iis7-application-pool-recycling-and-idle-time-out

关于asp.net-core - 无法在 ASP.NET Core 2.0 中增加 session 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50228068/

相关文章:

asp.net-core - ASP.NET CORE 应用程序的 appsettings.json 中的 Serilog 配置

c# - 如何从 SignalR 获取连接客户端的域

c# - asp.net Core 2 具有复杂模型的自定义模型绑定(bind)器

macos - "SharedCompilationId"任务不支持 ASP.NET CORE 2.1 Preview "Csc"参数

c# - .Net Core 2.0 未找到 View 'Index'

c# - HTTP 属性与路由相同 - ASP.Net Core API?

asp.net-core - 使用带有监听选项的 Kestrel

asp.net - DELETE 返回错误 405(方法不允许)我正在使用 React 和 ASP.Net Core

c# - 通过代码启动.NET Core Web API - 多平台(linux/windows)

c# - ASP.NET CORE 3.1 读取 session 时未分配初始值时出现 NULL 异常错误