08 Sep 2016
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:
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; } }
}
}
<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>
//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());
<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.