title: How do I force session timeout or a logout of a user when the app auto saves in an asp.net mvc 2 application?
body: <p>I've seen this question asked a few ways and the solutions are generally for other languages and don't apply to ASP.NET MVC 2.</p>
  
  <p>I am using Jquery &amp; Jquery forms to auto-save user data at a set interval. I still want the application to be able to time out, but the auto-saves via jquery forms keep refreshing the server.</p>
  
  <p>My initial idea to fix this was pretty simple. I've already got an ActionFilter I use to see if the session expires. Well, the session won't ever expire; however, I just keep track of how many auto saves occurr based on a value in session and when it reaches a limit (specified in the web.config), it does a:</p>
  
  <pre><code> filterContext.Result = new RedirectResult("~/Account.aspx/LogOn");
  </code></pre>
  
  <p>Well, this doesn't work because the auto save is doing an ajaxFormSubmit to call the action in the first place. I've tried changing the action to redirect to the login page, but the same thing happens....it just doesn't do a redirect. The only thing the action can return is a Json result. In my latest version (code below) I'm setting the json return value to false and calling a redirectToLogin() function to send the page over to the login page. It doesn't work and i'm not sure why.</p>
  
  <p>Any thoughts on this would be most helpful.</p>
  
  <p>Excerpt of code that sets up the interval for autosaving on the view (placed just before the form is closed):</p>
  
  <pre><code>&lt;%
      double sessionTimeoutInMinutes = double.Parse(ConfigurationManager.AppSettings["SESSION_TIMEOUT_IN_MINUTES"].ToString());
      double maxContiguousAutoSaves = double.Parse(ConfigurationManager.AppSettings["MAX_CONTIGUOUS_AUTO_SAVES"].ToString());           
      double autoSaveInterval = (sessionTimeoutInMinutes / maxContiguousAutoSaves) * 60 * 1000;               
  %&gt;
  
      &lt;%= Html.Hidden("autoSaveInterval", autoSaveInterval) %&gt;
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
              var autoSaveFrequency = $('[id=autoSaveInterval]').val();
              //alert(' Auto Save Interval in miliseconds: ' + autoSaveFrequency);                
              setInterval(
                  "initAutoSave('AutoSaveGoals', 'message')"
                  , autoSaveFrequency);
          });
  
      &lt;/script&gt;       
  </code></pre>
  
  <p>"AutoSaveGoals" goals is the name of one of my actions. It handles the post, updates certain items in session, and calls the repository.update. It is defined below: </p>
  
  <pre><code>   [HttpPost]
      public ActionResult AutoSaveGoals(Data data)
      {
          Data sessdata = Data();
          sessdata.MpaGoals = data.Goals;
          sessdata.MpaStatus = data.MpaStatus;
          sessdata.StartPeriodDate = data.StartPeriodDate;
          sessdata.EndPeriodDate = data.EndPeriodDate;
          sessdata.AssociatePassword = data.AssociatePassword;
  
          try
          {
              _repository.update(sessdata);
          }
          catch (Exception e)
          {
              LogUtil.Write("AutoSaveGoals", "Auto Save Goals Failed");
              LogUtil.WriteException(e);
          }
  
                      if (!autoLogOffUser(RouteData.GetRequiredString("action")))
              return Json(new { success = true });
          else
              return Json(new { success = false });
  
      }
  </code></pre>
  
  <p>The initAutoSave function is javascript that uses Jquery &amp; Jquery Forms plugin. Here it is:</p>
  
  <pre><code>function initAutoSave(targetUrl, messageDivId) {
      var options = {
          url: targetUrl,
          type: 'POST',
          beforeSubmit: showRequest,
          success: function(data, textStatus) {
              //alert('Returned from save! data: ' + data);
              if (data.success) {
                  var currDateAndTime = " Page last saved on: " + getCurrentDateAndTime();
                  $('[id=' + messageDivId + ']').text(currDateAndTime).show('normal', function() { })
              }
              else {
                  alert('redirecting to login page');
                  redirectToLogin();
                  //$('[id=' + messageDivId + ']').text(' An error occurred while attempting to auto save this page.').show('normal', function() { })
                  //alert('ERROR: Page was not auto-saved properly!!!!');
              }
          }
      };
      $('form').ajaxSubmit(options);
  }
  </code></pre>
  
  <p>I try doing a javascript redirect in redirectToLogin() but it doesn't seem to get the url or something behind the scenes is blowing up. Here is how it's defined:</p>
  
  <pre><code>function redirectToLogin() {    
      window.location = "Account.aspx/LogOn";
  }
  </code></pre>