Introduction
In this article, we will explain how to create a "Session State in ASP.NET Core and MVC Core".
Session State
In Session State, we can use to save and store user data while the user browses your web app. We already know that in previous versions of ASP.NET, we could store a session as key-value pair like this "Session["Name"] = "Rajeesh Menoth" and implement it in an easy way. But in the latest version of ASP.NET or ASP.NET Core, we need to do a few configurations for accessing and enabling Session State in the application. The main purpose of a session is to maintain user data in memory because HTTP is a stateless protocol.
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
- Getting Started With ASP.NET CORE 1.0
- Project Layout In ASP.NET Core 1.0
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part One
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part Two
Package Required
We need to install the stable version of “Microsoft.AspNetCore.Session” from Nuget Package Manager. Then only we can access Session State in ASP.NET Core 1.1.

.csproj
In “.csproj” we can check all the installed packages and version details in ASP.NET Core 1.1.
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
Assemblies Required
These are the assemblies mainly required for accessing the functionality of Session State, MVC, JSON, etc.
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
Home Controller
The following code is an example of a sharing session in ASP.NET Core 1.1.
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace SessionInCore.Controllers
{
public class HomeController : Controller
{
const string SessionKeyName = "_Name";
const string SessionKeyAge = "_Age";
const string SessionKeyDate = "_Date";
public IActionResult Index()
{
HttpContext.Session.SetString(SessionKeyName, "Rajeesh Menoth");
HttpContext.Session.SetInt32(SessionKeyAge, 28);
// Requires you add the Set extension method mentioned in the SessionExtensions static class.
HttpContext.Session.Set<DateTime>(SessionKeyDate, DateTime.Now);
return View();
}
public IActionResult About()
{
ViewBag.Name = HttpContext.Session.GetString(SessionKeyName);
ViewBag.Age = HttpContext.Session.GetInt32(SessionKeyAge);
ViewBag.Date = HttpContext.Session.Get<DateTime>(SessionKeyDate);
ViewData["Message"] = "Session State In Asp.Net Core 1.1";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "My Contact Details";
return View();
}
public IActionResult Error()
{
return View();
}
}
public static class SessionExtensions
{
public static void Set<T>(this ISession session, string key, T value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T Get<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
}


Priyanka KalePosted Mar 2, 2018, 2:17 AM
Great article, hey thanks for sharing!
Amit Kumar SinghPosted Jun 11, 2017, 1:00 AM
Nice one .... thanks for sharing