I've written a ASP.Net Core application that uses SQL based session storage. When I run the application via visual studio, the session object persists builds and starting/stopping IIS express, however, when I deploy it to the server (Win server 2012R2), an app pool recycle causes it to lose session state. - Edit, actually a restart of my computer, which obviously ends an underlying process, causes me to lose session in visual studio also.
My ConfigureServices:
// Serializer settings have been changed so that the JSON response sends the same case, not lower case first letter as default
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
// Builder to access the config
var builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
var config = builder.Build();
// Get and configure the session timeout
int SessionTimeout = Convert.ToInt32(config["AppSettings:SessionTimeout"]);
services.AddSession(s =>
{
s.IdleTimeout = TimeSpan.FromMinutes(SessionTimeout);
}
);
// Get the session state datebase location
var AspStateConnectionString = config["AppSettings:ConnectionStrings:ASPStateConnectionString"];
services.AddDistributedSqlServerCache(o =>
{
o.ConnectionString = AspStateConnectionString;
o.SchemaName = "dbo";
o.TableName = "Sessions";
});
My Configure:
// Configure to use session
app.UseSession();
// Pass context to CoreSessionManager class and ContextPerRequest
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
CoreSessionManager.Configure(httpContextAccessor);
CoreLibrary.ContextPerRequest.Configure(httpContextAccessor);
// Configure the routes - defaults to login page.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}");
});
The CoreSessionManager class does the work around the actual session objects:
private static IHttpContextAccessor HttpContextAccessor;
public static void Configure(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
public static UserDetail UserDetail
{
get
{
if (HttpContextAccessor.HttpContext.Session.Get("UserDetail") != null)
return JsonConvert.DeserializeObject<UserDetail>(HttpContextAccessor.HttpContext.Session.GetString("UserDetail"));
return null;
}
set
{
HttpContextAccessor.HttpContext.Session.SetString("UserDetail", JsonConvert.SerializeObject(value));
}
}
If this UserDetails is null, the user is kicked out to the login screen. Is there something I am missing? Do I need to add in some other configurations?
Thanks in advance, David