In this article I will explain, how to set Session Timeout value in ASP.Net Core application.
Unlike previous ASP.Net applications, the Session Timeout value is set inside the Startup.cs class in ASP.Net Core applications.
 
 
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.UseSession();
 
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            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)
{
    services.AddMvc();
 
    //Set Session Timeout. Default is 20 minutes.
    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromMinutes(30);
    });
}
 
 
Namespaces
You will need to import the following namespace.
using Microsoft.AspNetCore.Http;
 
 
Controller
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();
    }
 
    [HttpPost]
    public IActionResult Index(string sessionName)
    {
        //Get value from Session object.
        ViewData["Message"] = HttpContext.Session.GetString(sessionName);
        return View();
    }
}
 
 
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>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        Session:
        <input type="text" id="txtSessionName" name="SessionName"/>
        <input type="submit" value="Get Session"/>
    </form>
    @if (ViewData["Message"] != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert('@ViewData["Message"]');
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
Set Session Timeout in ASP.Net Core
 
 
Downloads