Difference between revisions of "Setup Session for ASP.Net Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Setting up Session for ASP.Net Core Web App= Using the nuget package manager you need to install `Microsoft.AspNetCore.Session`. The version I have used is 2.2.0. You can no...")
 
(Setting up Session for ASP.Net Core Web App)
Line 13: Line 13:
 
});
 
});
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
This code must be before the line `var app = builder.Build();` but after the line `var builder = WebApplication.CreateBuilder(args);`.
 +
 +
You now must add this:
 +
<syntaxhighlight lang=c#>
 +
app.UseSession();
 +
</syntaxhighlight>
 +
 +
It should go before your `app.Run()`.

Revision as of 13:34, 16 October 2024

Setting up Session for ASP.Net Core Web App

Using the nuget package manager you need to install `Microsoft.AspNetCore.Session`. The version I have used is 2.2.0.

You can now add the following code to your `program.cs` file:

builder.Services.AddDistributedMemoryCache();

builder.Services.AddSession(options =>
{
	options.IdleTimeout = TimeSpan.FromSeconds(10);
	options.Cookie.HttpOnly = true;
	options.Cookie.IsEssential = true;
});

This code must be before the line `var app = builder.Build();` but after the line `var builder = WebApplication.CreateBuilder(args);`.

You now must add this:

app.UseSession();

It should go before your `app.Run()`.