使用Javascript setTimeout调用MVC操作来保持活动表单身份验证

问题描述:

我使用setTimeout来调用MVC操作来尝试保持用户表单身份验证的活动状态。使用Javascript setTimeout调用MVC操作来保持活动表单身份验证

当我正常调用此代码(即不使用setTimeout)时,它会保持表单身份验证活着。

在setTimeout中调用时,它不会。

我的问题是为什么当通过setTimeout调用时不工作?

$(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 = "<%= Url.Content("~/Account/Logoff") %>" 
     window.location = URL; 
    } 
} 

function DoKeepAlive(){ 

     var URL = "<%= Url.Content("~/Account/ValidateAuthentication") %>" 
    $.post(
      //"../../Account/ValidateAuthentication",  
      URL, 
      function(data) { 

      } 
     ); 

} 
+1

缺少一些代码?什么是“promptUserTimeoutId”?另外,为什么你将“timeout”设置为“validationPrompt”函数? – 2011-06-10 07:43:17

+0

对不起亚历杭德罗,我试图分类我的代码,并把它弄得一团糟 - 现在应该更清楚了吗? – ozz 2011-06-10 08:36:23

经过多次来回发酵,我发现今天我使用的服务器上的时间慢了3分钟。在这个thread触发帮助我。

很难说为什么你的代码不工作。我会首先尝试简化它:

<script type="text/javascript"> 
    window.setInterval(function() { 
     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) { 
      $.post('<%= Url.Action("ValidateAuthentication", "Account") %>'); 
     } else { 
      window.location.href = '<%= Url.Action("Logoff", "Account") %>'; 
     } 
    }, 2 * 60 * 1000); 
</script> 
+0

嗨达林,感谢您的建议 - 同样的结果,但它不起作用在我的生活环境:-( – ozz 2011-06-10 09:17:15

+0

@james,你看到AJAX请求被发送?它们是否成功?您的表单身份验证令牌的超时在web.config中高于'2 * 60 * 1000'? – 2011-06-10 09:18:27

+0

他们确实发送了,但是我得到的响应是我的登录页面,所以它看起来像一个新的请求,没有经过身份验证,然后重定向到登录。超时比配置更高 – ozz 2011-06-10 09:26:31

尝试把timeout= setTimeout(validationPrompt, 2 * 60 * 1000);$(document).ready

+0

顺便说一句,你等了很多秒,看看它是否真的有用吗?这是一个很长的时间,等待,直到你找到:) – Shrinath 2011-06-13 13:21:44

+0

大声笑,我现在使用的时间要短得多:-) – ozz 2011-06-13 13:25:06

+0

尝试了你的建议,但它并没有帮助,无论如何。 – ozz 2011-06-13 13:32:06