logo

,

How to Enable Session in Asp.net Core 6 and Above

In this article, we will see how we can enable session in an Asp.net Core 6 MVC web application.

What is a Session?

Session is a feature in Asp.net Core that allows us to maintain or store the user data throughout the application. Session can store any type of object. In Session, you can store variable values or any other type of object such as list, datatable, the object of a class, etc.

Session stores the data on the server and a Session Id is used as a key, which is saved on the client-side as a cookie. This Session Id cookie is sent with every request. The Session Id cookie is unique to each browser and cannot be shared among browsers.

Session in Asp.net Core

Unlike previous versions of Asp.net Web Forms or Asp.net MVC, Session by default is not enabled in Asp.net Core. This can be done by performing some configuration.

Enable Session in Asp.net Core

I will create an Asp.net Core 6 MVC web application by the name “SessionDemo”.

Framework

Below is the screenshot of the project folder structure.

Folder Structure

Now, to enable session in our Asp.net Core MVC web application we need to do some configuration. For that, navigate to the Program.cs file in the project.

Don’t forget to check out: Startup.cs Missing in .Net 6 and Above

Go to the IServices collection and add the below line of code before the builder.Build() function:


builder.Services.AddDistributedMemoryCache();

The functionality of the above line of code is that it creates a default implementation of IDistributedCache. It allows us to store objects in memory. The Distributed Memory Cache isn’t an actual distributed cache. Cached items are stored by the app instance on the server where the app is running.

Now add the below line of code to add Session above the builder.Build() function.


builder.Services.AddSession();

We can even pass the Session options in the above. For example – setting idle time out (meaning the session will be abandoned after a certain idle time).


builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
});

Finally, add the UseSession middleware as below:


app.UseSession();

Below is how my Program.cs file looks after the Session configuration:

 

Set Session Value

To set the Session value use the below code:


HttpContext.Session.SetString("AnyKey", "Hello World!");

Now, as we have set the Session value, this value will be available throughout our application.

 

Read Session Value

To read the session value use the below code:


var sessionValue = HttpContext.Session.GetString("AnyKey");

Don’t forget to check out: Generate QR Code in Asp.net Core with 4 Simple Steps

Wrapping Up

I hope, this blog post has helped you in learning and understanding how to enable Session in Asp.net Core 6 and above. If you like it, share it with your friends and help this blog grow.

2 Responses

  1. Hola! I’ve been following your web site for a long time now and finally got the courage to go ahead and give you a shout out from Humble Tx! Just wanted to say keep up the good job!

Leave a Reply

Your email address will not be published.

Share on facebook
Share on twitter
Share on linkedin

Related articles