注册 登录
编程论坛 ASP.NET技术论坛

session有效期问题

shixqgirl 发布于 2010-10-22 08:58, 1131 次点击
作为一个服务器端的状态管理方式,怎么控制session的有效期,在web.config中设置后,不到时间就会结束会话,这个有效期都会被什么原因影响。
2 回复
#2
wangnannan2010-10-22 09:13
可以这么做
     <system.web>
      <authentication mode="Forms">
        <forms timeout="60"/>
      </authentication>

    ...
    </system.web>
尽管我们可以很简单的增加Session的过期时间,当用户真的离开的时候,它会让你的服务器系统浪费很多内存资源来保存一些完全没有意义的东西。如果这个网站的访问量非常大的时候,可能由于Session占用的内存太多,而使网站运行得很慢。
用javascript来保持Session
程序代码:
  <img id="imgSessionAlive" width="1" height="1" />  
   <script type="text/javascript" >    // Helper variable used to prevent caching on some browsers  
   var counter;    counter = 0;   
   function KeepSessionAlive()
   {  
      counter++;   
      var img = document.getElementById        
      ("imgSessionAlive");   
      img.src = "http://" + counter;   
      (KeepSessionAlive, 60000);
   }      
   KeepSessionAlive();  
  </script>
使用JQUERY 保持Session
程序代码:
<script language="javascript" type="text/javascript" src="http://code.">
</script>   
<script language="javascript" type="text/javascript">   
function KeepSessionAlive()
{   
   ("http://");   
   setInterval(KeepSessionAlive, 60000);
}   
   KeepSessionAlive();
</script>

 使用Meta Refresh来保持Session
<iframe height="0" width="0" src="RefreshSessionState.aspx" frameborder="0" />

 此时RefreshSessionState.aspx如果你不要跟踪用户的话不需要服务器代码。我习惯于这样写

使用 ajax 来保持Session
程序代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax-Refresh.aspx.cs" Inherits="Ajax_Refresh" %>   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.">   
<html xmlns="http://www.">   
<head runat="server">   
<title></title>   
</head>   
<body>   
<form id="form1" runat="server">   
<asp:ScriptManager ID="ScriptManager1" runat="server">   
</asp:ScriptManager>   
<div>   
<asp:UpdatePanel ID="UpdatePanel1" runat="server">   
<ContentTemplate>   
<asp:Timer ID="Timer1" runat="server" Interval="10000" ontick="Timer1_Tick">   
</asp:Timer>   
<asp:Label ID="Label1" runat="server" Text="Label">
</asp:Label>   
</ContentTemplate>   
</asp:UpdatePanel>   
</div>   
</form>   
</body>   
</html>

实际上在许多公司使用的比较少,但是如果是一些要求快速开发的项目来说, ajax也不愧为一个很好的选择。
以上就是保持延长Session的四种解决方案,希望对你有些帮助。








#3
shixqgirl2010-10-22 09:24
回复 2楼 wangnannan
好的,谢谢!
1