我无法在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");
}
});
}
答案 0 :(得分:1)
问题可能出现,因为您的应用程序池在IIS上被回收。默认情况下,IIS应用程序池每20分钟就会被回收一次。
尝试增加应用程序池的循环时间。
更改应用程序池循环时间。请浏览以下链接
https://www.coreblox.com/blog/2014/12/iis7-application-pool-recycling-and-idle-time-out
答案 1 :(得分:0)
我使用asp.net core 2.2(在没有IIS的kestrel中自托管),并且在某些会话变量上存在相同的问题(它们在大约20-30分钟后被重置)。
在startup.cs中,我有:
services.AddSession();
(没有选项,只要用户保持连接,我便会自动保留会话)
我将其更改为:
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(8);
});
而且...这似乎可行(在Kestrel(生产环境)和IIS-Express(调试)环境中。)
关于回收(如上所述),根据此帖子:
[茶est回收:] [1] Kestrel webserver for Asp.Net Core - does it recycle / reload after some time
茶est似乎不回收。