Using Session in ASP.NET CORE 3.0
As we all know all web application work on HTTP protocol and this protocol is stateless to maintain state, we need to use State Management techniques.
Below are 2 types of State Management techniques of server-side and client-side.
Server-side
- Session state
- TempData, ViewBag, View Data
- Cache
Let’s start with Understanding Server-side Methods
Session
A session is a key component in the web application which helps to maintain state and session are stored on the server.
We have been using session from long-time from ASP to ASP.NET MVC now we have Session in ASP.NET Core too but syntax and way to use keep changing but the core mechanics remain same.
For showing demo, I am going to start with creating an ASP.NET Core Web Application with Name “WebApplication2“.

Once you have created a project you will see the entire project structure in Solution Explorer.

Now you have created project let’s learn how to configure application such that we can use Session in it.
For using Session in ASP.NET core we need to add service of the session to ConfigureServices Method which is located in Startup.cs class.
Added Session Service in ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddControllersWithViews();
}

For adding service, we have used services.AddSession method and in that we have configured a few options.
Idletimeout option: – how long session will be ideal before it expires and the only session will expire in this not cookie if you are using a cookie.
HttpOnly: – if we set cookie HTTP Only attribute to true then we cannot access cookie through a client-side script. For securing application it should always be set to true.
IsEssential: – Is this cookie essential for your application if yes then set IsEssential to true else false.
Adding Session Middleware
Now we are going to configure Session Middleware in Startup.Configure Method just by adding app.UseSession(); before app.UseMvc Middleware.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}

Now we have configured session in this application let’s create session, store some values in session and retrieve values from it.
Creating Session
We are going to create a session for doing that we are going to use Home Controller which has the Index Action method.
For creating Session, we need to import namespace Microsoft.AspNetCore.Http after importing
We are going set Session for setting session we have 2 SessionExtensions one for setting string value and another for setting integer value.
First, let’s use SetString SessionExtensions for storing the value in the session.

Setting Session (storing)
Key: – “DemoSession”
Value: – “Hi Session”
HttpContext.Session.SetString(“DemoSession”, “Hi Session”);
Source Code
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
HttpContext.Session.SetString("DemoSession", "Hi Session");
return View();
}
public IActionResult Privacy()
{
return View();
}
}
}
Reading Stored Session Value
For retrieving value which is stored in session we are going to use GetString SessionExtensions.

Key: – “DemoSession”
string value = HttpContext.Session.GetString(“DemoSession”);
Source Code
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
// Storing Value
HttpContext.Session.SetString("DemoSession", "Hi Session");
// Retrieving Value
string value = HttpContext.Session.GetString("DemoSession");
return View();
}
public IActionResult Privacy()
{
return View();
}
}
}
Similar way we can store integer values and retrieve it using GetInt32 SessionExtensions.
// Storing Value
HttpContext.Session.SetInt32("DemoSessionInteger", 99);
// Retrieving Value
int? integervalue = HttpContext.Session.GetInt32("DemoSessionInteger");
Now Let’s Run the project and see values in debug mode.
