What is it all about?

This post shows you how to create a keep alive for a .NET website so people can stay logged in whilst the page is open, instead of it timing out after 20 minutes.

The basic premise to this is:

  • Create a handler which responds with an OK message and doesn't allow caching.
  • Add some javascript to the page to call the handler every 10 minutes.
  • The call to the server side handler keeps the session alive.

Here's how to do it:

  1. Create a folder in your web project called Handlers, if you don't have one already.
  2. Add a class called KeepAlive.cs
  3. Add the following code to the KeepAlive class
using System.Web;
using System.Web.SessionState;
namespace CodeShare.Web.Handlers
public class KeepAlive : IHttpHandler, IRequiresSessionState
public void ProcessRequest(HttpContext context)
context.Response.AddHeader("Cache-Control", "no-cache");
context.Response.AddHeader("Pragma", "no-cache");
context.Response.ContentType = "text/plain";
public bool IsReusable { get { return false; } }
  1. In the web.config file at the root of the site, register this handler. 
<remove name="KeepAlive" />
<add name="KeepAlive" verb="GET" path="keep-alive.ashx" type="CodeShare.Web.Handlers.KeepAlive " />
  1. Create a javascript file in your scripts or js folder called keepAlive.js 
//calls the keep alive handler every 600,000 miliseconds, which is 10 minutes
var keepAlive = {
refresh: function () {
setTimeout(keepAlive.refresh, 6000000);
}; $(document).ready(keepAlive.refresh());
  1. Reference the keepAlive.js file in your page, like this:
<script src="/scripts/keepAlive.js"></script>

Or you could inlcude it in your script bundle, see my post about how to use bundling and minification in MVC and Umbraco.

If you are using this with Umbraco, you will need to add the address of the keep alive handler to the umbracoReservedPaths key in the appSettings.

<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/bundles/,~/keep-alive.ashx" />

That's it, the javascript will keep calling the handler every 10 minutes or however long you set, and that will keep the session alive.

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by


  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.

  • Custom stickers for startups, artists and brands.
  • Bespoke easy-apply, removable, custom brand stickers printed in the UK.

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.