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";
context.Response.Write("OK");
}
public bool IsReusable { get { return false; } }
}
}
  1. In the web.config file at the root of the site, register this handler. 
<configuration>
<system.webServer>
<handlers>
<remove name="KeepAlive" />
<add name="KeepAlive" verb="GET" path="keep-alive.ashx" type="CodeShare.Web.Handlers.KeepAlive " />
</handlers>
</system.webServer>
</configuration>
  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 () {
$.get('/keep-alive.ashx');
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

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

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

  • 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.
uSkinned

  • 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.
UmbHost

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