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.
- 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) {
-
- 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)
- {
-
- HttpContext.Session.Timeout = 31;
- }
- else
- {
-
- 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?