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.
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.
This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…
Read PostThis post gives you some razor code to help you see the values of the IPublishedContent item's prope…
Read PostIn this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…
Read PostThis post will help you fix the issue where you csv export has corrupt characters when opening it in…
Read Post