Oh the wonderful MVC world....
I am trying to setup a timeout script for my website. Currently it seems to work but if the user wanted to extend the timeout time then move to another page it seems to have lost all of my session varibles I had saved.
FilterConfig.cs code:
public class IsAuthenticatedAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (rowCnt >= 1) //Making sure user has permissions to even view this site
{
System.Diagnostics.Debug.WriteLine("Current timeout: " + HttpContext.Current.Session.Timeout);
System.Diagnostics.Debug.WriteLine("Session timeout: " + HttpContext.Current.Session["timeoutDate"]);
if (HttpContext.Current.Session.Timeout == 20) //20 is the default timeout setting
{
//It's the first time the user has visited the page so set timeout!
filterContext.HttpContext.Session.Clear();
filterContext.HttpContext.Session.RemoveAll();
HttpContext.Current.Session.Timeout = 1;
HttpContext.Current.Session["sessionVars"] = new initSessionVars().getSVars;
sessionVars sVars = HttpContext.Current.Session["sessionVars"] as sessionVars;
//Send them to the main page in order to populate the sessions and needed data
filterContext.Result = new RedirectResult("/");
} else
{
if (HttpContext.Current.Session["timeoutDate"] == null)
{
//No timeout has been saved so save it
int sessionTimeout = HttpContext.Current.Session.Timeout;
HttpContext.Current.Session["timeoutDate"] = DateTime.Now.AddMinutes(sessionTimeout);
}
}
} else
{
//User was not found so they are NOT good to go!
filterContext.HttpContext.Response.Redirect("~/Account/Unauthorized");
}
}
}
I am using some jQuery in order to check for the timeout and present the user with a message box 10 seconds before it resets the session and sends them to a "sessions over" page.
_Layout.cshtml code:
var idleTime = 0;
var secondsLeft = 10;
var sTimer;
var idleInterval;
$(document).ready(function () {
idleInterval = setInterval(timerIncrement, @Html.Raw((Session.Timeout * 60) * 1000));
$(".sleepy-close, .sleepy-wake-up").click(function () {
$(".sleepy-overlay").fadeOut('fast');
clearInterval(sTimer);
idleTime = 0;
$.ajax({
url: 'Home/sessionChk',
type: 'POST',
cache: false,
dataType: 'json',
data: {
continueSession: true
},
success: function (responce) {
console.log('done');
idleInterval = setInterval(timerIncrement, responce);
setVars();
},
error: function (responce) {
showNotify('error', 'An error of<br/>' + responce + '<br/>Has occurred. Please get in touch with me about this error.', 20000);
}
});
});
$('.sleepy-modal').click(function (event) {
event.stopPropagation();
});
});
function setVars() {
idleTime = 0;
secondsLeft = 10;
$('#timer').html(secondsLeft);
}
function timerIncrement() {
idleTime++;
if (idleTime > 1) {
$('.sleepy-overlay').fadeIn('slow');
clearInterval(idleInterval);
sessionTimer();
}
}
function sessionTimer() {
sTimer = setInterval(function () {
$('#timer').html(secondsLeft);
secondsLeft--;
if (secondsLeft <= 0) {
clearInterval(sTimer);
$.ajax({
url: 'Home/sessionChk',
type: 'POST',
cache: false,
dataType: 'json',
data: {
continueSession: false
},
success: function (responce) {
//clear the full screen for user to input new data
console.log('done');
},
error: function (responce) {
showNotify('error', 'An error of<br/>' + responce + '<br/>Has occurred. Please get in touch with me about this error.', 20000);
}
});
}
}, 1000);
}
If the user clicks the "stay awake" button then it sends an AJAX request to Home/sessionChk which sends a true for the continueSession.
HomeController.cs sessionChk code:
public class HomeController : Controller
{
[HttpPost]
public void sessionChk(bool continueSession)
{
ActionExecutingContext filterContext = new ActionExecutingContext();
if (continueSession)
{
//Add more time to the session timeout
HttpContext.Session.Timeout = 31;
}
else
{
//Lets go to the sessions exspiered page
filterContext.Result = new RedirectResult("/");
}
}
}
So after it adds 31 more mintues I go ahead and pick another page within the site to visit. Once that page starts loading it tells me a session varible I am trying to load is no longer there.....
During the 1 minute the session starts when the page loads for the first time I can surf to any page within the site and all the sessions are fine and load up with the correct data so I know they have data at the beginning.
So what am I doing incorrectly since it doesnt seem to safe the session data when I extend the timeout time?