How can we detect session timeout in .net core and on detection redirect to login page ?
My application is multi tenant application, for which I am using sasskit library. So, after resolving the teanant I am keeping the TenantContext in session.
If my application goes into idle state and after that If I try to pull the data from database the it gives the TenantContext null is error and all its properties are null.
Below is my code
Startup
public void ConfigureServices(IServiceCollection services)
{
// Add multitenant
services.AddMultitenancy<AppTenant, CachingAppTenantResolver>();
services.Configure<Settings>(Configuration.GetSection("Settings"));
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
// add Dbcontext
services.AddEntityFrameworkSqlServer().AddDbContext<LMSContext>();
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<LMSContext, int>()
.AddDefaultTokenProviders();
services.AddMemoryCache();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(1);
});
services.AddSqlLocalization(options => options.UseTypeFullNames = true);
services.AddOptions();
services.AddMvc(o =>
{
o.Filters.Add(new LanguageActionFilter());
})
.AddViewLocalization()
.AddDataAnnotationsLocalization()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.Configure<MultitenancyOptions>(Configuration.GetSection("Multitenancy"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMultitenancy<AppTenant>();
}
``````
public class LMSContext : IdentityDbContext
{
private readonly AppTenant tenant;
public LMSContext(AppTenant tenant)
{
this.tenant = tenant;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(tenant.ConnectionString); // Here I am getting the tenant null
base.OnConfiguring(optionsBuilder);
}
}
Here not able to get it from session and thus the tenant is not injected in DBContext
protected override Task
{
string strPath = string.Empty;
TenantContext<AppTenant> tenantContext = context.Session.GetObjectFromJson<TenantContext<AppTenant>>("TenantContext");
}
```
Any help on this appreciated !
/cc @Tratcher @sebastienros
The short answer is that when you detect missing data / a new empty session then it's safe to assume the prior session timed out.
@Tratcher what about checking the expiration in the data store IDistributedCache?
@hishamco the app doesn't have access to that information, it's maintained by the external store.
I see, probably your previous suggestion is simple answer for the timeout
Closing as this was answered here: https://github.com/aspnet/Mvc/issues/6126#issuecomment-293917933
@rynowak here is here or not an object! :(
Closing as this was answered here: #6126 (comment)
This is not a necessarily correct answer in that there are other reasons why a new/empty session is returned (e.g. application restart without an external cache). I would also want to explicitly know when a session has timed out as opposed to simply losing session for other reasons, but I don't see a way to do it with the standard interfaces.