In this article I will explain with an example, how to get and set Session object (variable) in ASP.Net Core MVC.
Startup.cs Configuration
Enabling the Session
Session can be enabled using the Configure method.
Inside this method, you will have to call the UseSession method of the app object.
Note: It is mandatory to call the UseSession method before the UseMvc method.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    //Enable Session.
    app.UseMvc(routes =>
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
Setting the Session Timeout
Inside this method, you will have to call the AddSession method of the services object.
The AddSession can be called directly without any parameters and it can also be used to set the IdleTimeout property which sets the Session Timeout duration.
Note: The default Session Timeout in ASP.Net Core is 20 minutes.
public void ConfigureServices(IServiceCollection services)
    //Set Session Timeout. Default is 20 minutes.
    services.AddSession(options =>
        options.IdleTimeout = TimeSpan.FromMinutes(30);

You will need to import the following namespace.
using Microsoft.AspNetCore.Http;
The Controller consists of the following two Action methods.
Action method for handling GET operation
Inside this Action method, Session object is set and the View is returned.
The Session object is set using the SetString method of the HttpContext.Session property.
Action method for handling POST operation
When the Get Session Button is clicked, the following Action method is executed.
Inside this Action method, the Session variable name is received as parameter. Then the value of the Session object is fetched and assigned to ViewData object.
Finally, the View is returned.
public class HomeController : Controller
    public IActionResult Index()
        //Set value in Session object.
        HttpContext.Session.SetString("Person", "Mudassar");
        return View();
    public IActionResult Index(string sessionName)
        //Get value from Session object.
        ViewData["Message"] = HttpContext.Session.GetString(sessionName);
        return View();
The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of an HTML TextBox and a Submit Button.
When the Get Session Button is clicked, the Form gets submitted and the value of TextBox is sent to the Controller.
Finally, the value of the Session object is displayed using JavaScript Alert Message Box.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <form method="post" asp-controller="Home" asp-action="Index">
        <input type="text" id="txtSessionName" name="SessionName"/>
        <input type="submit" value="Get Session"/>
    @if (ViewData["Message"] != null)
        <script type="text/javascript">
            window.onload = function () {
Get and Set Session object (variable) in ASP.Net Core