I am rather new to dotnet and I am trying to leave a session cookie in the browser and set its timespan.
This is my Program.cs, more or less taken from tutorials and StackOverflow answers I found:
using MyApp.Utils;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddRazorPages();
builder.Services.AddSession(options => // Is executed after app.UseSession ();
{
options.IdleTimeout = TimeSpan.FromMinutes(30); // Set session timeout
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true; // Make the session cookie essential
options.Cookie.MaxAge = TimeSpan.FromDays(365);
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession(); // Is executed before builder.Services.AddSession ( options... )
app.UseAuthorization();
app.MapRazorPages();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
EnvironmentSettings.GetSettings(); // Gets settings from INI file.
app.UseHttpsRedirection();
app.UseAuthentication();
app.MapControllers();
app.UseDefaultFiles();
app.MapRazorPages();
app.Run();
This is all I see in the browser:
Can anyone see what I am doing wrong?
If it helps, I noticed that app.UseSession is executed before builder.Services.AddSession( options => … )