65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 会话超时

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (38投票s)

2011 年 7 月 18 日

CPOL

2分钟阅读

viewsIcon

284998

downloadIcon

11636

本文探讨了警告用户会话超时的方法。

引言

我的项目中的一个需求是警告用户会话即将过期。虽然对于最终用户来说,这似乎是一个简单的需求,但对于开发人员和设计师来说并非如此。我们需要处理实际应用程序中的许多场景。实现这个目标的最佳方法是什么? 一些挑战包括:

  1. 会话是一个滑动过期值。每次有回发时,它都会被延长。
  2. 您可以处理此场景的多种方法,每种方法都有其自身的技术挑战。

方法

以下部分将尝试介绍几种处理会话过期的方法。

1. 提供简单的警告

在这种方法中,用户将根据预定义的时间间隔收到简单的警告消息。

<script language="javascript" type="text/javascript">
       var sessionTimeoutWarning = 
	"<%= System.Configuration.ConfigurationSettings.AppSettings
	["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";

        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

        function SessionWarning() {
var message = "Your session will expire in another " + 
	(parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + 
	" mins! Please Save the data before the session expires";
alert(message);
        }
</script>
  • sessionTimeoutWarning:是应用程序配置中预定义的值。例如 18 分钟。
  • sessionTimeout:保存会话超时间隔。例如 20 分钟。如果用户在页面上没有进行大约 18 分钟的回发操作,他将收到有关会话即将过期的警告。

2. 提供简单的警告,然后将用户重定向到主页或登录页

  <script language="javascript" type="text/javascript">
        var sessionTimeoutWarning = 
	"<%= System.Configuration.ConfigurationSettings.AppSettings
	["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";
        var timeOnPageLoad = new Date();
 
        //For warning
        setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
        //To redirect to the welcome 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 welcome page");
                window.location = "../Welcome.aspx";
            }
        }

        //Session timeout
        function RedirectToWelcomePage(){
            alert("Session expired. You will be redirected to welcome page");
            window.location = "../Welcome.aspx";
        }
  </script>	

在这种方法中,用户将收到有关会话超时的警告。如果用户没有保存或进行任何回发操作,一旦会话间隔时间到期,他将被重定向到登录或主页。

3. 延长用户会话

 <script language="javascript" type="text/javascript">
        var sessionTimeoutWarning = 
	"<%= System.Configuration.ConfigurationSettings.AppSettings
	["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";
        var timeOnPageLoad = new Date();
        var sessionWarningTimer = null;
        var redirectToWelcomePageTimer = null;
        //For warning
        var sessionWarningTimer = setTimeout('SessionWarning()', 
				parseInt(sessionTimeoutWarning) * 60 * 1000);
        //To redirect to the welcome page
        var redirectToWelcomePageTimer = 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. Do you want to extend the session?";

            //Confirm the user if he wants to extend the session
            answer = confirm(message);

            //if yes, extend the session.
            if(answer)
            {
                var img = new Image(1, 1);
                img.src = 'KeepAlive.aspx?date=' + escape(new Date());

                //Clear the RedirectToWelcomePage method
                if (redirectToWelcomePageTimer != null) {
                    clearTimeout(redirectToWelcomePageTimer);
                }
   	       //reset the time on page load
                timeOnPageLoad =  new Date();
                sessionWarningTimer = setTimeout('SessionWarning()', 
				parseInt(sessionTimeoutWarning) * 60 * 1000);
                //To redirect to the welcome page
                redirectToWelcomePageTimer = setTimeout
		('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
            }

            //*************************
            //Even after clicking ok(extending session) or cancel button, 
	   //if the session time is over. Then exit the session.
            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 welcome page");
                window.location = "../Welcome.aspx";
            }
            //**************************
        }

        //Session timeout
        function RedirectToWelcomePage(){
            alert("Session expired. You will be redirected to welcome page");
            window.location = "../Welcome.aspx";
        }
</script>   

在这种方法中,用户将收到有关会话超时的警告,并提供延长用户会话的能力。如果用户确认延长会话,则会话将被延长。如果用户在会话过期超时限制后确认,即使如此,用户也将被注销。以下代码行用于延长用户会话。其中 'KeepAlive.aspx' 是网站中的一个虚拟页面。

var img = new Image(1, 1); 
img.src = 'KeepAlive.aspx?date=' + escape(new Date()); 

注意:在上述所有场景中,我假设 SetTimeout 方法和会话相关变量将在每次回发时重置。当可能存在部分渲染并且 SetTimeout 方法和会话相关变量可能未重置时,这可能无法 100% 工作。所有文件都在Samples文件夹中。

参考文献

© . All rights reserved.