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.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

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.

Related Posts

How to render the actual document type icons in the tree in Umbraco

This post gives you the code to be able to see the icons for your document types in the tree view ra…

Read Post

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to set the default page base type to UmbracoViewPage in Umbraco

In this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…

Read Post