【问题标题】:Not able to increase session timeout in ASP.NET Core 2.0无法在 ASP.NET Core 2.0 中增加会话超时
【发布时间】:2018-05-08 07:21:50
【问题描述】:

我无法在 ASP.NET Core 2.0 中增加会话超时。会话在每 20 到 30 分钟后过期。当我将时间减少到 1 分钟并对其进行调试时,它工作正常,但是当它增加到超过 30 分钟或几小时/天时,它不会持续到指定的持续时间。

在调试模式和 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");
            }
        });
    }

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 但是当我在调试模式下使用 VS 2017 和 IIS express 运行应用程序时,我遇到了同样的问题。
    • 您是否更改了此选项。IdleTimeout = TimeSpan.FromMinutes(180);从你的代码到更高的东西并重新测试它。
    • 是的,我已将其更改为 2 天、3 天,但 20 到 30 分钟后会话仍然超时。
    • 感谢 Trilok Kumar。将应用程序池空闲时间更改为 60 后,现在可以正常工作了
    【解决方案2】:

    我使用 asp.net core 2.2(在 kestrel 中自托管,没有 IIS) 并且在某些会话变量上遇到了同样的问题(它们在大约 20-30 分钟后被重置)。

    在 startup.cs 中,我有:

    services.AddSession();
    

    (没有选项,因为我认为只要用户有连接,会话就会自动存在)

    我已将其更改为:

    services.AddSession(options =>
    {
       options.IdleTimeout = TimeSpan.FromHours(8);
    });
    

    而且...它似乎可以工作(在 Kestrel(生产)和 IIS-Express(调试)中)。

    关于回收(如上所述),根据这篇帖子:

    [红隼回收:][1] Kestrel webserver for Asp.Net Core - does it recycle / reload after some time

    Kestrel 似乎不回收。

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 1970-01-01
      • 2020-07-23
      • 2017-09-17
      • 2014-03-26
      • 2018-11-05
      • 2012-01-29
      • 2012-03-12
      • 1970-01-01
      相关资源
      最近更新 更多