Asp Net Core 2.0 Session not saved

Are you using Asp Net Core 2.0 Session and the context.Session.Id is changing each page ?

The solution is easy, just set the session options in Startup.cs as below, in particular:

options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;

public void ConfigureServices(IServiceCollection services)
{
         
    services.AddDistributedMemoryCache(); 
    services.AddSession(options =>
    {
        options.Cookie.HttpOnly = true;
        options.Cookie.Name = ".Your.Session";
        options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
        options.IdleTimeout = TimeSpan.FromMinutes(10);
    });
}

Leave a Reply

Your email address will not be published. Required fields are marked *