title: Using Javascript setTimeout to call an MVC action to keep alive forms authentication
body: <p>I am using setTimeout to call an MVC action to try and keep alive the users forms authentication.</p>
  
  <p>When I call this code normally (ie not using setTimeout) it DOES keep the forms authentication alive.</p>
  
  <p>When called within setTimeout it does not. </p>
  
  <p><strong>My question is why does this not work when called via setTimeout?</strong></p>
  
  <pre><code>$(document).ready(function() {
  
      DoKeepAlive();    //simple test when page loads....does keep forms auth alive
  });
  
  
  var timeout= 0;
  timeout= setTimeout(validationPrompt, 2 * 60 * 1000);  
  
  function validationPrompt() {
  
      var answer = confirm("Your session will timeout in less than a minute. Click OK to stay logged in, and reset the timout period.")
      if (answer) {
  
          DoKeepAlive();     //when called in here by setTimeout, does NOT keep forms auth alive
  
          //re-set the 10 minutes count
          clearTimeout(timeout);
          timeout= setTimeout(validationPrompt, 2 * 60 * 1000); 
      }
      else {
          var URL = "&lt;%= Url.Content("~/Account/Logoff") %&gt;"
          window.location = URL;
      }
  }
  
  function DoKeepAlive(){
  
          var URL = "&lt;%= Url.Content("~/Account/ValidateAuthentication") %&gt;"
      $.post(
              //"../../Account/ValidateAuthentication",      
              URL,
              function(data) {
  
              }
          );
  
  }
  </code></pre>