I have my ASP.Net application uploaded on a shared host (godaddy plesk windows server). The Idle timeout on the shared host is set to 5 minutes and cannot be changed.
I have heard that the session timer can be reset by pinging the server before the timeout period has elapsed and I am trying to do this but my code doesn't seem to work. I am using this as a guideline:
Keeping ASP.NET Session Open / Alive (The post by Maryan) and
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
Basically in my Home controller I have put this code:
[HttpPost]
public JsonResult KeepSessionAlive()
{
return new JsonResult {Data = "Success"};
}
In my Scripts folder I have put this script titled SessionUpdater.js
SessionUpdater = (function () {
var clientMovedSinceLastTimeout = true; //I want it to be always on so if statement fires
var keepSessionAliveUrl = null;
var timeout = 1 * 1000 * 60; // 1 minutes
function setupSessionUpdater(actionUrl) {
// store local value
keepSessionAliveUrl = actionUrl;
// setup handlers
listenForChanges();
// start timeout - it'll run after n minutes
checkToKeepSessionAlive();
}
function listenForChanges() {
$("body").one("mousemove keydown", function () {
clientMovedSinceLastTimeout = true;
});
}
// fires every n minutes - if there's been movement ping server and restart timer
function checkToKeepSessionAlive() {
setTimeout(function () { keepSessionAlive(); }, timeout);
}
function keepSessionAlive() {
// if we've had any movement since last run, ping the server
if (clientMovedSinceLastTimeout == true) {
$.ajax({
type: "POST",
url: keepSessionAliveUrl,
success: function (data) {
// reset movement flag
clientMovedSinceLastTimeout = true; //always on
// start listening for changes again
listenForChanges();
// restart timeout to check again in n minutes
checkToKeepSessionAlive();
},
error: function (data) {
console.log("Error posting to " & keepSessionAliveUrl);
}
});
}
}
// export setup method
return {
Setup: setupSessionUpdater
};
})();
And then in both my layout page and in my home\index page (in case partial view doesnt work for some reason) I put this reference:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
My Problem is that despite doing this the session still logs me out every 5 minutes of being idle and I can tell that the app pool recycles because the website takes forever to fire up again. What am I doing wrong? How can I get this to stop logging me out.
Any information or being pointed in the right direction is appreciated.