In this article you will learn following things:
- What is Session?
- How many ways for Session storage?
- How to Enable Session in .Net 8 Asp.Net Core MVC Application?
- How to set or store the value in session?
- How to get or retrieve the value of session?
- How to remove particular session key?
- How to remove all session keys?
- Walk-through on session implementation.
What is a Session?
Session is KEY-VALUE structure to store the user specific data. In Asp.Net Core .NET 8 session is not enable by default, we have to enable it. Session data persists across multiple HTTP requests.
How many ways for Session storage?
- In-Memory Cache: Server base storage. Storage capacity is depend upon server’s available memory. Best for single server base environment.
- Distributed Cache: Session storage not on one server its store on multiple server. Distributed cache work with Redis, Sql Server etc. .
How to Enable Session in .Net 8 Asp.Net Core MVC Application?
Add another line just after app declaration line:
How to set or store the value in session?
Create or Setting Session value.
Syntax
Example
How to get or retrieve the value of session?
Getting Session value.
Syntax
Example
How to remove particular session key?
You can easily remove a specific session key.
Syntax
Example
How to remove all session keys?
You can delete/remove/clear all sessions in one go.
Clear: It remove all the current session entries from the server.
OR
SignOutAsync: It end the current user session and remove or clear the user’s authentication cookie from the server.
Walk-through on session implementation
In this article I had used Visual Studio 2022. After follow step by step you are able to implement SESSION in your Asp.Net Core project.
![Create a new project]()
![Configure a new project]()
![Additional information]()
![AspNet-Core-Net8-Working-With-Session]()
Above screenshot is a view of default project structure of Asp.Net Core .NET 8.
Now open program.cs file to add following line:
Update Program.cs file Code:
Open the HomeController.cs file from CONTROLLERS folder.
Update HomeController.cs file code:
Open the Privacy.cshtml file from VIEWS àHOME.
Update the Privacy.cshtml file code :
Now press F5 to run the project.
Output
![Asp.Net-Core-Net8-Working-With-Session]()
Happy Coding!