locked
Session Time Out Question in Core RRS feed

  • Question

  • User2029849071 posted

    Hello,

    In my ASP.NET Core MVC solution, I currently have jquery that gets called when the user selects a selection in a drop down control via the onchange event in the select element.  Based on the user selection the jquery then displays a view in a div element.

    A user has told me that after a period of time when the page is idle when he comes back to use the page and makes a selection in the dropdown, nothing happens and he has to refresh the page for the functionality to work again.  

    I did some testing to investigate and he was correct.  In Chrome after 20 min of idle, the dropdown did not initiate the jquery call.  In Fire Fox, after 1 hour of idle the dropdown did not initiate the jquery call.  I did each test 5 times and got the same results both browsers.  I'm having problems figuring out if this is caused by a ASP.NET core session state, based on browser or both.

    I read the following: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.0 and was wondering if I needed to configure the session state so the functionality does not time out after 20 minutes, but wanted some advise before doing so.  Any help would be appreciated.

    Thanks in advance,

    Tim

    Friday, November 15, 2019 11:45 PM

Answers

  • User-474980206 posted

    most likely the jQuery is doing an ajax call which is getting and error, and you have no error handling. 

    as session can timeout, you should really code to recover the session data. session should be treated like a cache, it needs a backing store, and you shoudl be able to recreate on a miss.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2019 8:44 PM

All replies

  • User1289604957 posted

    Hello,

    Would you please share your Strartup.cs file?

    Best regards,

    Maher

    Saturday, November 16, 2019 10:58 AM
  • User2029849071 posted

    Here is my Startup.cs.

    Thanks,

    Tim


    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using DinkToPdf; using DinkToPdf.Contracts; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Server.IISIntegration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Newtonsoft.Json.Serialization; using OnTargetLibrary.Security; using PDF_Generator.Utility; namespace OnTarget { public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { IServiceProvider serviceProvider = services.BuildServiceProvider(); IHostingEnvironment env = serviceProvider.GetService<IHostingEnvironment>(); services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools())); var customAssemblyLoadContext = new CustomAssemblyLoadContext(); customAssemblyLoadContext.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll")); services.AddDistributedMemoryCache(); services .AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); services.AddAuthorization(options => { options.AddPolicy("Administration", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Administration - Intranet"))); options.AddPolicy("Manufacturing", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Manufacturing - Intranet"))); options.AddPolicy("Sales", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Sales - Intranet"))); options.AddPolicy("Shipping", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Shipping - Intranet"))); options.AddPolicy("Supply Chain", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Supply Chain - Intranet"))); options.AddPolicy("Warranty", policy => policy.Requirements.Add(new IntranetSecurityGroupRequirement("Warranty - Intranet"))); }); services.AddSingleton<IAuthorizationHandler, IntranetSecurityGroupHandler>(); services.AddAuthentication(IISDefaults.AuthenticationScheme); // Add Kendo UI services to the services container services.AddKendo(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.Use(async (ctx, next) => { await next(); if (ctx.Response.StatusCode == 403 && !ctx.Response.HasStarted) { //Re-execute the request so the user gets the error page string originalPath = ctx.Request.Path.Value; ctx.Items["originalPath"] = originalPath; ctx.Request.Path = "/Error/Forbidden"; await next(); } else if (ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted) { //Re-execute the request so the user gets the error page string originalPath = ctx.Request.Path.Value; ctx.Items["originalPath"] = originalPath; ctx.Request.Path = "/Error/PageNotFound"; await next(); } else if (ctx.Response.StatusCode == 500 && !ctx.Response.HasStarted) { //Re-execute the request so the user gets the error page string originalPath = ctx.Request.Path.Value; ctx.Items["originalPath"] = originalPath; ctx.Request.Path = "/Error/AppError"; await next(); } else { app.UseExceptionHandler("/Error/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } }); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { //need route and attribute on controller: [Area("Blogs")] routes.MapRoute(name: "mvcAreaRoute", template: "{area:exists}/{controller=Home}/{action=Index}"); // default route for non-areas routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }

    Saturday, November 16, 2019 10:32 PM
  • User1289604957 posted

    Hello Tim,

    Thank you for the share.

    I'm having problems figuring out if this is caused by an ASP.NET core session state, based on browser or both.

    It is related to ASP.NET Core session timeout which is 20 minutes by default.

    To prove it is 20 minutes, let's put a breakpoint options.IdleTimeout

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddRazorPages();
                services.AddSession(options =>
                {
                    options.IdleTimeout = TimeSpan.FromSeconds(5);
                });
            }

    Best regards,

    Maher

    Sunday, November 17, 2019 1:42 PM
  • User1289604957 posted

    Do not forget to add app.UseSession(); in your Configure method so that the breakpoint will work.

    Best regards,

    Maher

    Sunday, November 17, 2019 1:45 PM
  • User1289604957 posted

    F5:

    F10:

    Best regards,

    Maher

    Sunday, November 17, 2019 3:26 PM
  • User2029849071 posted

    Can you explain what setting the FromSeconds to 5 does to the Session? 

    Thanks,

    Tim

    Tuesday, November 19, 2019 8:33 PM
  • User-474980206 posted

    most likely the jQuery is doing an ajax call which is getting and error, and you have no error handling. 

    as session can timeout, you should really code to recover the session data. session should be treated like a cache, it needs a backing store, and you shoudl be able to recreate on a miss.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2019 8:44 PM
  • User2029849071 posted

    You were correct this was the issue.  The jQuery was doing an ajax call and the session timed out after 20 min no longer able to make the call.

    I decided based on the use case, to keep the session alive and used a jQuery with ajax call to call a controller method to keep the session alive.  It calls the method every 10 min and works great for the use case.

    Thanks.

    Monday, December 9, 2019 4:47 PM