Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are using ASP.NET MVC – you do not need an additional HTTP handler and some modifications of the web.config file. All you need – just to add some simple action in a Home/Common controller:</p> <pre><code>[HttpPost] public JsonResult KeepSessionAlive() { return new JsonResult {Data = "Success"}; } </code></pre> <p>, write a piece of JavaScript code like this one (I have put it in one of site’s JavaScript file):</p> <pre><code>var keepSessionAlive = false; var keepSessionAliveUrl = null; function SetupSessionUpdater(actionUrl) { keepSessionAliveUrl = actionUrl; var container = $("#body"); container.mousemove(function () { keepSessionAlive = true; }); container.keydown(function () { keepSessionAlive = true; }); CheckToKeepSessionAlive(); } function CheckToKeepSessionAlive() { setTimeout("KeepSessionAlive()", 300000); } function KeepSessionAlive() { if (keepSessionAlive &amp;&amp; keepSessionAliveUrl != null) { $.ajax({ type: "POST", url: keepSessionAliveUrl, success: function () { keepSessionAlive = false; } }); } CheckToKeepSessionAlive(); } </code></pre> <p>, and initialize this functionality by calling a JavaScript function:</p> <pre><code>SetupSessionUpdater('/Home/KeepSessionAlive'); </code></pre> <p><strong>Please note!</strong> I have implemented this functionality only for authorized users (there is no reason to keep session state for guests in most cases) and decision to keep session state active is not only based on – is browser open or not, but authorized user must do some activity on the site (move a mouse or type some key).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload