Session登陆后丢失的解决办法。

1。打开web.config文件,设置如下:

<!--  会话状态设置
          默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。
          如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
         若要禁用 Cookie,请设置 sessionState cookieless="true"。
    -->
    <sessionState
            mode="StateServer"             //就是修改这里为“StateServer”
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false"
            timeout="30"
    />

2。打开“控制面板--管理工具--服务”,找到ASP.NET State Service这个服务,将其设为自动启动。

None.gif  蛙蛙推荐:一套.net窗体身份验证方案(解决了防止用户重复登陆,session超时等问题) 
None.gif 
None.gif一.      设置web.config相关选项 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif先启用窗体身份验证和默认登陆页,如下。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
< authentication mode = " Forms " >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif       
< forms loginUrl = " default.aspx " ></ forms >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
</ authentication >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif设置网站可以匿名访问,如下 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
< authorization >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif           
< allow users = " * "   />  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
</ authorization >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif     
< location path = " admin " >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif       
< system.web >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif           
< authorization >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif              
< deny users = " ? " ></ deny >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif           
</ authorization >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif       
</ system.web >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
</ location >  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
< globalization requestEncoding = " gb2312 "  responseEncoding = " gb2312 "   />  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif设置session超时时间为1分钟,并启用cookieless,如下。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
< sessionState mode = " InProc "  cookieless = " true "  timeout = " 1 "   />  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
< trace enabled = " true "  requestLimit = " 1000 "  pageOutput = " true "  traceMode = " SortByTime "  localOnly = " true "   />  
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif二.      设置Global.asax文件 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif处理Application_Start方法,实例化一个哈西表,然后保存在Cache里 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif    
protected   void  Application_Start(Object sender, EventArgs e) 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       Hashtable h
=new Hashtable(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       Context.Cache.Insert(
"online",h); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif    在Session_End方法里调用LogoutCache()方法,方法源码如下 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif    
/// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用      /// </summary> 

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif    
public   void  LogoutCache() 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       Hashtable h
=(Hashtable)Context.Cache["online"]; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
if(h!=null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(h[Session.SessionID]!=null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           h.Remove(Session.SessionID); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           Context.Cache[
"online"]=h; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif三.      设置相关的登陆和注销代码 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache[
" online " ]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法. 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 防止用户重复登陆,在用户将要身份验证前使用 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif       
/// <param name="name">要验证的用户名字</param> 

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif       
private   void  PreventRepeatLogin( string  name) 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           Hashtable h
=(Hashtable)Cache["online"]; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(h!=null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              IDictionaryEnumerator e1
=h.GetEnumerator(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
bool flag=false
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
while(e1.MoveNext()) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{                 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                  
if((string)((ArrayList)e1.Value)[0]==name) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                     flag
=true
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                     
break
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                  }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif              }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
if(flag) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                  TimeSpan ts
=System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[1])); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                  
if(ts.TotalSeconds<60
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                     
throw new oa.cls.MyException("对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                  
else 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                     h.Remove(e1.Key);        
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif              }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
else 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              h
=new Hashtable(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           ArrayList al
=new ArrayList(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           al.Add(name); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           al.Add(System.DateTime.Now); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           h[Session.SessionID]
=al; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(Cache["online"]==null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              Context.Cache.Insert(
"online",h); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
else 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              Cache[
"Online"]=h;          
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif    }
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif用户注销的时候调用上面提到LogoutCache()方法 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif四.      设置admin目录下的的所有页面的基类 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System.Web; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System.Web.UI; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System.Web.UI.WebControls; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System.Web.UI.HtmlControls; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System.Collections; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif
namespace  oa.cls 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
public class MyBasePage : System.Web.UI.Page 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
protected bool IsAdminDir 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
get 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
return Request.FilePath.IndexOf("/oa/admin")==0
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
private void PreventSessionTimeout() 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(!this.IsAdminDir) return
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(Session["User_Name"]==null&&this.IsAdminDir) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{             
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              System.Web.Security.FormsAuthentication.SignOut(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
this.Alert("登陆超时",Request.ApplicationPath) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用. 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
private void UpdateCacheTime() 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           Hashtable h
=(Hashtable)Cache["online"]; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(h!=null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              ((ArrayList)h[Session.SessionID])[
1]=DateTime.Now; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           Cache[
"Online"]=h; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// <param name="myList"></param> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
private void TraceValues( Hashtable myList)  
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           IDictionaryEnumerator myEnumerator 
= myList.GetEnumerator(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
int i=0
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
while ( myEnumerator.MoveNext() ) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              Context.Trace.Write( 
"onlineSessionID"+i, myEnumerator.Key.ToString()); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              ArrayList al
=(ArrayList)myEnumerator.Value; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              Context.Trace.Write( 
"onlineName"+i, al[0].ToString()); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              Context.Trace.Write( 
"onlineTime"+i,al[1].ToString()); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              TimeSpan ts
=System.DateTime.Now.Subtract(Convert.ToDateTime(al[1].ToString())); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif               Context.Trace.Write(
"当前的时间和此登陆时间间隔的秒数",ts.TotalSeconds.ToString()); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              i
++
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 弹出信息并返回到指定页 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// <param name="msg">弹出的消息</param> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// <param name="url">指定转向的页面</param> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
protected void Alert(string msg,string url) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
string scriptString = "<script language=JavaScript>alert(\""+msg+"\");location.href=\""+url+"\"</script>"
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(!this.IsStartupScriptRegistered("alert")) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
this.RegisterStartupScript("alert", scriptString); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
/// 这个方法也在下面的OnInit方法里调用 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif       
protected void XmlReLoad() 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{      
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           System.Text.StringBuilder htmlstr
=new System.Text.StringBuilder(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"<SCRIPT LANGUAGE=\"JavaScript\">"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"function GetMessage(){"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"  var xh=new ActiveXObject(\"Microsoft.XMLHTTP\");"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"  xh.open(\"get\",window.location,false);"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"  xh.send();"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"  window.setTimeout(\"GetMessage()\",60000);"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"}"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"window.οnlοad=GetMessage();"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           htmlstr.Append(
"</SCRIPT>       "); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(!this.IsStartupScriptRegistered("xmlreload")) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
this.RegisterStartupScript("alert", htmlstr.ToString()); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif       
override protected void OnInit(EventArgs e) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
base.OnInit(e); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
this.PreventSessionTimeout(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
this.UpdateCacheTime(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
this.XmlReLoad(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
if(this.Cache["online"]!=null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif              
this.TraceValues((System.Collections.Hashtable)Cache["online"]); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif五.      写一个自定义异常类 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
using  System; 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif 
None.gif
None.gif
None.gif
namespace  oa.cls 
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
/// MyException 的摘要说明。 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
public class MyException:ApplicationException 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// 构造函数 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           
/// </summary> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
public MyException():base() 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// 构造函数 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           
/// <param name="ErrMessage">异常消息</param> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
public MyException(string Message):base(Message) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif               System.Web.HttpContext.Current.Response.Redirect(
"~/ShowErr.aspx?msg="+Message); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
/**//// <summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// 构造函数 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
/// <param name="Message">异常消息</param> 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           
/// <param name="InnerException">引起该异常的异常类</param> 

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif           
public MyException(string Message,Exception InnerException):base(Message,InnerException) 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif           }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif       }
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif六.总结 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif我发现在Session里保存的值,比如session[
" name " ]是没有任何向服务器的请求达到1分钟后就会自动丢失,但是session ID是关闭同一进程的浏览器页面后达1分钟后才会丢失并更换的,因为只要你开着浏览器就会有session ID,无论是在url里保存还是在cookies里。不知道这个结论对不对,反正我在设置了session的timeout为1分钟后,session[ " name " ]的值已经没有了,可是SessionID还是旧的,Global.asax里的Session_End里的代码也没有执行,而身份验证票据也没有丢失。我不知道这三者之间的关系是怎样的,谁先谁后,好像在 < authentication > 小节可以设置一个timeout属性,不过项目赶的紧,我没时间研究了。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif以上这些代码比较零散,我花费了2天的时间才总结出来这套方案,不是很完美,但是暂时只能这样了,不能在这方面浪费很多时间了,大家可以把上面的代码组织到一个类里,然后把方法都修改成静态方法方便调用。 
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif最后大家有什么建议和改进的意见欢迎和我交流。 
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值