解决Session过期问题的方法

解决Session过期问题的方法

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

http://community.csdn.net/Expert/topic/3100/3100218.xml?temp=.4426386
还有可能就是你在测试期间改动了,网站的文件。

我测试过没有问题!测试程序如下:
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里面的