2013-05-08 125 views
6

有什么方法可以知道在超时发生之前会话对象剩余了多长时间?获取时间留在会话中,直到超时

我知道如何从webconfig获取实际的超时时间,但有什么方法可以知道会话最后一次活动的时间吗?

我的用户将在我的应用程序中填写大型表单,如果在他们点击“提交”之前超过10分钟,他们将被注销。如果会议即将超时,我想警告他们。我将使用ajax调用进行检查。

+0

这可能是对你有用的:http://programmerramblings.blogspot.nl/2009/08/aspnet- session-timeout-control-jquery.html – 2013-05-08 09:21:36

回答

2

您可以在一个javascript变量中跟踪服务器时间DateTime.Now
用JavaScript比较当前时间。

如果差异达到阈值限制,您可以在javascript中显示警告消息。

更好的是,您可以触发ajax调用您的服务器,这将延长您的服务器超时。

+0

只是想建议一样的。检查http://forums.asp.net/t/1207721.aspx/2/10 – StringBuilder 2013-05-08 09:24:25

+2

假设每次对服务器进行的调用(同步或异步)都会“重置”超时值是否安全? – Johan 2013-05-08 10:03:34

+2

是的。用户对服务器的任何请求将重置超时 – nunespascal 2013-05-08 10:31:33

2

您还可以通过后面的代码将Session超时设置为JavaScript超时。让它运行你想要的任何JavaScript函数。非常灵活,你不需要用检查当前时间渣土约...等

事情是这样的:

"window.setTimeout('yourJSFunctionGoesHere()', " + Session.Timeout + ");"; 

如果你想提醒前2分你可能确实是这样的:

"window.setTimeout('yourWarningJSFunctionGoesHere()', " + Session.Timeout-2 + ");"; 
+0

这看起来不错。除了我更喜欢传递一个自治函数到'setTimeout()';) – Johan 2013-05-08 10:05:14

+0

谢谢我需要代表:-) – TheKingDave 2013-05-08 11:21:29

+0

http://weknowgifs.com/wp-content/uploads/2013/03/christian-bale-upvote -gif.gif – Johan 2013-05-08 19:26:21

2

我们可以通过定义在web.config中的会话时间&预警时间,并使用定时器来确认页面上的经过时间处理这个问题。

的Web.Config:

<appSettings> 
    <add key="SessionWarning" value="30" /> 
</appSettings> 

<system.web> 
    <sessionState timeout="45"/> 
</system.web> 

客户端页面:

<script language="javascript" type="text/javascript"> 
    /* Page Time Out Warning Message Script */ 
    var sessionTimeoutWarning = '<%=System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>'; 
    var sessionTimeout = "<%= Session.Timeout %>"; 
    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000; 

    var timeOnPageLoad = new Date(); 
    setTimeout('SessionWarning()', sTimeout); 
    //To redirect to the home page 
    setTimeout('RedirectToWelcomePage()', parseInt(sessionTimeout) * 60 * 1000); 

    //Session Warning 
    function SessionWarning() { 
     //minutes left for expiry 
     var minutesForExpiry = (parseInt(sessionTimeout) - 
              parseInt(sessionTimeoutWarning)); 
     var message = "Your session will expire in another " + minutesForExpiry + 
           " mins! Please Save the data before the session expires"; 
     alert(message); 
     var currentTime = new Date(); 
     //time for expiry 
     var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() 
              + parseInt(sessionTimeout)); 

     //Current time is greater than the expiry time 
     if (Date.parse(currentTime) > timeForExpiry) { 
      alert("Session expired. You will be redirected to home page"); 
      window.location = "../Home.aspx"; 
     } 
    } 

    //Session timeout add home page where you want to redirect after session timeout 
    function RedirectToWelcomePage() { 
     alert("Session expired. You will be redirected to home page"); 
     window.location = "../Home.aspx"; 
    } 
    /* End */ 

</script>