Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript setTimeout调用MVC操作以保持窗体身份验证的有效性_Javascript_Asp.net Mvc 2_Jquery_Forms Authentication - Fatal编程技术网

使用Javascript setTimeout调用MVC操作以保持窗体身份验证的有效性

使用Javascript setTimeout调用MVC操作以保持窗体身份验证的有效性,javascript,asp.net-mvc-2,jquery,forms-authentication,Javascript,Asp.net Mvc 2,Jquery,Forms Authentication,我正在使用setTimeout调用MVC操作,以尝试保持用户窗体身份验证的活动状态 当我正常调用此代码(即不使用setTimeout)时,它确实会使表单身份验证保持活动状态 当在setTimeout内调用时,它不会 我的问题是,当通过setTimeout调用时,为什么这不起作用? $(document).ready(function() { DoKeepAlive(); //simple test when page loads....does keep forms auth a

我正在使用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) {

            }
        );

}
$(文档).ready(函数(){
DoKeepAlive();//加载页面时的简单测试……是否保持窗体身份验证处于活动状态
});
var超时=0;
超时=设置超时(validationPrompt,2*60*1000);
函数validationPrompt(){
var answer=confirm(“您的会话将在不到一分钟内超时。单击“确定”以保持登录,并重置超时时间。”)
若有(答复){
DoKeepAlive();//当setTimeout在这里调用时,不会使窗体auth保持活动状态
//重新设置10分钟计数
clearTimeout(超时);
超时=设置超时(validationPrompt,2*60*1000);
}
否则{
var URL=“”
window.location=URL;
}
}
函数DoKeepAlive(){
var URL=“”
美元邮政(
//“../../Account/ValidateAuthentication”,
网址,
功能(数据){
}
);
}

很难说为什么代码不起作用。我会先尝试简化它:

<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>

window.setInterval(函数(){
var answer=confirm(“您的会话将在不到一分钟内超时。单击“确定”保持登录,并重置超时时间。”);
若有(答复){
$邮政('');
}否则{
window.location.href='';
}
}, 2 * 60 * 1000);

尝试放置
timeout=setTimeout(validationPrompt,2*60*1000)
$(文档)中。准备好了吗?

经过反复的翻腾,我发现今天我使用的服务器上的时间慢了3分钟。关于这一点的帖子帮助了我。

缺少一些代码?什么是“promptUserTimeoutId”?还有,为什么要将“超时”设置为函数“validationPrompt”?对不起,Alejandro,我试图命名我的代码,把它弄得一团糟-现在应该更清楚了吗?嗨,Darin,谢谢你的建议-尽管结果相同,但它在我的实时环境中不起作用:-(@james,你看到发送的AJAX请求了吗?它们成功了吗?web.config中表单身份验证令牌的超时是否高于
2*60*1000
?它们确实被发送了,但我得到的响应是我的登录页面。因此,看起来像是一个未经身份验证的新请求,然后重定向到登录。是的,超时是高于配置中的值。@james,您启用了slidingExpiration吗?@darin-不,它没有明确设置,但我相信默认值为true。顺便说一句,您是否等了那么多秒才知道它是否真的起作用了?这要等很长时间才能发现:)