session过期很快的解决方法(这个比较好)

我是在租用的虚拟主机上发现,自己在本地测试时正常的代码,在虚拟主机上session仅保存了几秒钟就过期了,登陆信息被清空,要重新登陆。

在页面中设置session.timeout=20(IIS的默认值也是20分钟)不管用,和虚拟主机服务商沟通也是枉然,最后发现了一种可以强制设定过期时间的方法,在global.asa中设置:

<script   language=vbscript   runat=server>  
 sub Session_OnStart  
  session.timeout=60   '默认是20(分钟)你设置的大一些就好了  
 end sub  
</script>

问题就解决了。

另外,提供一些抄来的参考资料:

SessionState 的Timeout),其主要原因有三种。
一:有些杀病毒软件会去扫描您的Web.Config文件,那时Session肯定掉,这是微软的说法。
二:程序内部里有让Session掉失的代码,及服务器内存不足产生的。
三:程序有框架页面和跨域情况。
第一种解决办法是:使杀病毒软件屏蔽扫描Web.Config文件(程序运行时自己也不要去编辑它)
第二种是检查代码有无Session.Abandon()之类的。
第三种是在Window服务中将ASP.NET State Service 启动。
还有可能就是你在测试期间改动了,网站的文件。


我测试过没有问题!测试程序如下:
protected void Application_Start(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Application_Start"+DateTime.Now.ToLongTimeString()+"application id"+Application.ToString()),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();

}
  
    protected void Session_Start(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Session_Start"+DateTime.Now.ToLongTimeString()+"SessionID="+Session.SessionID),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close(); 
    }

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Application_BeginRequest"+DateTime.Now.ToLongTimeString()),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();

    }

    protected void Application_EndRequest(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Application_EndRequest"+DateTime.Now.ToLongTimeString()),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();

    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_Error(Object sender, EventArgs e)
    {

    }

    protected void Session_End(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Session_End"+DateTime.Now.ToLongTimeString()+"SessionID="+Session.SessionID),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close(); 
    }

    protected void Application_End(Object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("server=localhost;database=YourDatabase;uid=sa;pwd=;");
      SqlCommand cmd = new SqlCommand(string.Format("insert into table1 (c) values('{0}')","Application_End"+DateTime.Now.ToLongTimeString()),con);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close(); 
    }

所有的代码都是Global里面的

.net下的配置如下:

sessionState节点的语法是这样的:

<    sessionState mode ="Off|InProc|StateServer|SQLServer"
               cookieless
="true|false"
               timeout
="number of minutes"
               stateConnectionString
="tcpip=server:port"
               sqlConnectionString
="sql connection string"
               stateNetworkTimeout
="number of seconds"
/>

必须有的属性是

属性 选项 描述
mode   设置将Session信息存储到哪里
Off 设置为不使用Session功能
InProc 设置为将Session存储在进程内,就是ASP中的存储方式,这是默认值。
StateServer 设置为将Session存储在独立的状态服务中。
SQLServer 设置将Session存储在SQL Server中。

可选的属性是:

属性 选项 描述
cookieless   设置客户端的Session信息存储到哪里
ture 使用Cookieless模式
false 使用Cookie模式,这是默认值。
timeout   设置经过多少分钟后服务器自动放弃Session信息。默认为20分钟
stateConnectionString   设置将Session信息存储在状态服务中时使用的服务器名称和端口号,例如:"tcpip=127.0.0.1:42424”。当mode的值是StateServer是,这个属性是必需的。
sqlConnectionString   设置与SQL Server连接时的连接字符串。例如"data source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"。当mode的值是SQLServer时,这个属性是必需的。
stateNetworkTimeout   设置当使用StateServer模式存储Session状态时,经过多少秒空闲后,断开Web服务器与存储状态信息的服务器的TCP/IP连接的。默认值是10秒钟。 

打开某个应用程序的配置文件Web.config后,我们会发现以下这段:

<    sessionState
  
mode ="InProc"
  stateConnectionString
="tcpip=127.0.0.1:42424"
  sqlConnectionString
="data source=127.0.0.1;Trusted_Connection=yes"
  cookieless
="false"
  timeout
="20"   
/>

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值