繁体   English   中英

只要用户处于活动状态,asp.net C# 就保持 session 处于活动状态

[英]asp.net C# keep the session alive as long as the user is active

I made a simple web app using asp.net C# and I made a code to logout automatically from the website and redirect the user to the login page when the session expire, but I have a problem which is the website logout and redirect the user to the login页面即使用户处于活动状态并单击按钮并移动鼠标,在这种情况下,我希望 session 像大多数网站一样在用户在超时期间不做任何事情时使其处于活动状态并使其过期。

这是我结束 session 的代码:

web.config:

<sessionState timeout="1"></sessionState>

main_page.aspx:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    AutoRedirect();
}

public void AutoRedirect()
{
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 6000);
    string str_Script = @"
           <script type='text/javascript'> 
               intervalset = window.setInterval('Redirect()'," +
                   int_MilliSecondsTimeOut.ToString() + @");
               function Redirect()
               {
                   window.location.href='/login.aspx'; 
               }
           </script>";

    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script);
}

先不说应用程序的超时如何在 web.config 中设置 session 超时

在你的 Javascript 我认为你需要停止 setInterval 停止 setInterval 调用 JavaScript

那么为什么不使用 aspx 页面的 javascript 中的所有任务

<script>

$(document).ready(function)(){
     ResetTheTimer();
     $('body').mousemove(function() { // or other events
           ResetTheTimer();
     });

});
var intervalset ;
var MilliSecondsTimeOut = xxxx;
function ResetTheTimer(){
    
    clearInterval(refreshIntervalId);
    intervalset = window.setInterval(Redirect,  MilliSecondsTimeOut );
             
}
  function Redirect()
  {   window.location.href='/login.aspx'; 
  }
 </script>

然后为了让应用程序活跃起来,你也需要一些时间间隔来回发到后面的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM