In this article I will explain with an example, how to set Session Timeout value in ASP.Net Core Razor Pages application.
Unlike previous ASP.Net applications, the Session Timeout value is set inside the Startup.cs class in ASP.Net Core Razor Pages applications.
Note: For beginners in ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Hello World Tutorial with Sample Program example.
 
 
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)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    //Enable Session.
    app.UseSession();
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseMvc();
}
 
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 Razor Pages is 20 minutes.
 
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 
    //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;
 
 
Razor PageModel (Code-Behind)
The PageModel consists of two Handler methods.
Handler method for handling GET operation
Inside this Handler method, Session object is set.
The Session object is set using the SetString method of the HttpContext.Session property.
 
Handler method for handling Button Click and POST operation
When the Get Session Button is clicked, the following Handler method is executed.
Inside this Handler method, the Session variable name is received as parameter. Then the value of the Session object is fetched using GetString method of the HttpContext.Session property and assigned to ViewData object.
public class IndexModel : PageModel
{
    public void OnGet()
    {
        //Set value in Session object.
        HttpContext.Session.SetString("Person", "Mudassar");
    }
 
    public void OnPostSubmit(string sessionName)
    {
        //Get value from Session object.
        ViewData["Message"] = HttpContext.Session.GetString(sessionName);
    }
}
 
 
Razor Page (HTML)
The HTML of Razor Page consists of an HTML Form with a TextBox element and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
 
When the Get Session Button is clicked, the Form gets submitted and the value of TextBox is sent to the PageModel.
Finally, the ViewData object named Message is checked for NULL and if it is not NULL then the value of Session object is displayed using JavaScript Alert Message Box.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Enable_Session_Core_Razor.Pages.IndexModel
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        Session:
        <input type="text" id="txtSessionName" name="SessionName" />
        <input type="submit" value="Get Session" asp-page-handler="Submit" />
    </form>
    @if (ViewData["Message"] != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert('@ViewData["Message"]');
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
ASP.Net Core Razor Pages: Set Session Timeout Value
 
 
Downloads