2017-09-11 71 views
0

下面的方法在15分钟后注销用户。但问题是即使用户处于活动状态,它也会将他注销。15分钟后自动注销c#

我正在寻找解决方案,当用户不是活动整整15分钟的方法它会记录他的方法,而不是该方法将不会运行。

public void AutoRedirect() 
{ 
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 900000); 
    string str_Script = @" 
    <script type='text/javascript'> 
    intervalset = window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + @"); 
    function Redirect() 
    { 
     alert('Your session has been expired and system redirects to login page now.!\n\n'); 
     window.location.href='../index.aspx'; 
    } 
    </script>"; 
    UtilityClass.RemoveCookie("login", Response); 
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script); 
} 
+0

每次页面称为检查会话var包含页面上次导航到的时间。如果少于15分钟,则使用当前时间更新该会话变量并允许用户继续。否则,将会话变量清空并将其重定向到主页。 –

回答

1

,而不是与一个单定时器运行15分钟后,怎么样设定的时间间隔为1分钟的情况下这样做倒计数器?

该计数器可以很容易地被其他事件重置,例如, AJAX调用,点击事件等,这些事件可以通过jQuery bv .delegate.on方法轻松触发。

如果没有发生此类重置,则倒数达到0,您可以重定向。

var inactivityCountdown = 15; 

intervalid = window.setInterval(function Redirect() 
{ 
    inactivityCountdown--; 
    if (inactivityCountdown<1) { 
    clearInterval(intervalid); 
    alert('Your session has been expired and system redirects to login page now.!\n\n'); 
    window.location.href='../index.aspx'; 
    } 
}, 60*1000); 

可以看出,这种方式很容易过期前的任何时间重置计数器inactivityCountdown。不要忘记在重定向时清除间隔。

在下面的代码片段中,我使用了10秒而不是几分钟来进行更好的测试。在文本字段中输入内容,或点击按钮重置倒计时。

你会得到的消息,然后注销文本,你就可以通过链接再次登录(此模拟重定向):

var initCountdownValue = 10; 
 
var inactivityCountdown = initCountdownValue; 
 
$(document).ready(function() { 
 

 
    $(document).delegate("#login", "click", function(e) { 
 
    location.reload(); 
 
    }); 
 

 
    function Logout() { 
 
    document.getElementById('mainpage').style.display = "none"; 
 
    document.getElementById('loggedout').style.display = ""; 
 
    // or window.location.href="/logout.aspx"; 
 
    } 
 

 
    var intervalid = window.setInterval(function Redirect() { 
 
    inactivityCountdown--; 
 
    $("#counter").text(inactivityCountdown.toString()); 
 
    if (inactivityCountdown < 1) { 
 
     clearInterval(intervalid); 
 
     Logout(); 
 
     alert('Your session has been expired and system redirects to login page now.!'); 
 
    } 
 
    }, 1 * 1000); 
 

 
    $(document).delegate(":input", "click change keydown", function() { 
 
    inactivityCountdown = initCountdownValue; 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<div id="mainpage" style=""> 
 
    <input type="text"></input> 
 
    <input type="button" text="test"></input> 
 
    <div> 
 
    Counter: 
 
    <div id="counter"></div> 
 
    </div> 
 
</div> 
 
<div id="loggedout" style="display:none"> 
 
    You are logged out. To login, click <a id="login" href="#">here</a> 
 
</div>

+0

伟大的一件事我怎样才能显示在ASP标签的计数器值谢谢 – Ayman

+0

不知道如果我明白你的意思,但我已修改了片段,所以它显示在屏幕上的倒计时。 – Matt

+0

感谢您的帮助 – Ayman

0

因此,window.setInterval不会像您认为的那样被覆盖。 你可以删除这个函数,让你的IIS或通过配置来处理它。

或者你可以清除的时间间隔,并重新设置它,在这里说: How do I stop a window.setInterval in javascript?