一套.net窗体身份验证方案(解决了防止用户重复登陆,session超时等问题)(我还没有实验过)

  1.  一. 设置web.config相关选项
  2. 先启用窗体身份验证和默认登陆页,如下。
  3. <authentication mode="Forms">
  4. <forms loginUrl="default.aspx"></forms>
  5. </authentication>
  6. 设置网浏览可以匿名访问,如下
  7. <authorization>
  8. <allow users="*" />
  9. </authorization>
  10. 然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
  11. <location path="admin">
  12. <system.web>
  13. <authorization>
  14. <deny users="?"></deny>
  15. </authorization>
  16. </system.web>
  17. </location>
  18. 把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
  19. <globalization requestEncoding="gb2312" responseEncoding="gb2312" />
  20. 设置session超时时间为1分钟,并启用cookieless,如下。
  21. <sessionState mode="InProc" cookieless="true" timeout="1" />
  22. 为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
  23. <trace enabled="true" requestLimit="1000" pageOutput="true" traceMode="SortByTime" localOnly="true" />
  24. 二. 设置Global.asax文件
  25. 处理Application_Start方法,实例化一个哈西表,然后保存在Cache里
  26. protected void Application_Start(Object sender, EventArgs e)
  27. {
  28. Hashtable h=new Hashtable();
  29. Context.Cache.Insert("online",h);
  30. }
  31. 在Session_End方法里调用LogoutCache()方法,方法源码如下
  32. /// <summary> 
  33. /// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用 /// </summary> 
  34. public void LogoutCache()
  35. {
  36. Hashtable h=(Hashtable)Context.Cache["online"];
  37. if(h!=null)
  38. {
  39. if(h[Session.SessionID]!=null)
  40. h.Remove(Session.SessionID);
  41. Context.Cache["online"]=h;
  42. }
  43. }
  44. 三. 设置相关的登陆和注销代码
  45. 登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
  46. /// <summary> 
  47. /// 防止用户重复登陆,在用户将要身份验证前使用 
  48. /// </summary> 
  49. /// <param name="name">要验证的用户名字</param> 
  50. private void PreventRepeatLogin(string name)
  51. {
  52. Hashtable h=(Hashtable)Cache["online"];
  53. if(h!=null)
  54. {
  55. IDictionaryEnumerator e1=h.GetEnumerator();
  56. bool flag=false;
  57. while(e1.MoveNext())
  58. if((string)((ArrayList)e1.Value)[0]==name)
  59. {
  60. flag=true;
  61. break;
  62. }
  63. }
  64. if(flag)
  65. {
  66. TimeSpan ts=System.DateTime.Now.Su浏览ract(Convert.ToDateTime(((ArrayList)e1.Value)[1]));
  67. if(ts.TotalSeconds<60)
  68. throw new oa.cls.MyException("对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!");
  69. else
  70. h.Remove(e1.Key); 
  71. }
  72. }
  73. else
  74. {
  75. h=new Hashtable();
  76. }
  77. ArrayList al=new ArrayList();
  78. al.Add(name);
  79. al.Add(System.DateTime.Now);
  80. h[Session.SessionID]=al;
  81. if(Cache["online"]==null)
  82. {
  83. Context.Cache.Insert("online",h);
  84. }else
  85. Cache["Online"]=h; 
  86. }
  87. 用户注销的时候调用上面提到LogoutCache()方法
  88. 四. 设置admin目录下的的所有页面的基类
  89. using System;
  90. using System.Web;
  91. using System.Web.UI;
  92. using System.Web.UI.WebControls;
  93. using System.Web.UI.HtmlControls;
  94. using System.Collections;
  95. namespace oa.cls
  96. {
  97. public class MyBasePage : System.Web.UI.Page
  98. {
  99. /// <summary> 
  100. /// 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录 
  101. /// </summary> 
  102. protected bool IsAdminDir
  103. {
  104. get
  105. {
  106. return Request.FilePath.IndexOf("/oa/admin")==0;
  107. }
  108. }
  109. /// <summary> 
  110. /// 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页 
  111. /// </summary> 
  112. private void PreventSessionTimeout()
  113. {
  114. if(!this.IsAdminDir) return;
  115. if(Session["User_Name"]==nullthis.IsAdminDir)
  116. System.Web.Security.FormsAuthentication.SignOut();
  117. this.Alert("登陆超时",Request.ApplicationPath)
  118. }
  119. }
  120. /// <summary> 
  121. /// 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用. 
  122. /// </summary> 
  123. private void UpdateCacheTime()
  124. {
  125. Hashtable h=(Hashtable)Cache["online"];
  126. if(h!=null)
  127. {
  128. ((ArrayList)h[Session.SessionID])[1]=DateTime.Now;
  129. }
  130. Cache["Online"]=h;
  131. }
  132. /// <summary> 
  133. /// 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据 
  134. /// </summary> 
  135. /// <param name="myList"></param> 
  136. private void TraceValues( Hashtable myList) 
  137. {
  138. IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
  139. int i=0;
  140. while ( myEnumerator.MoveNext() )
  141. {
  142. Context.Trace.Write( "onlineSessionID"+i, myEnumerator.Key.ToString());
  143. ArrayList al=(ArrayList)myEnumerator.Value;
  144. Context.Trace.Write( "onlineName"+i, al[0].ToString());
  145. Context.Trace.Write( "onlineTime"+i,al[1].ToString());
  146. TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(al[1].ToString()));
  147. Context.Trace.Write("当前的时间和此登陆时间间隔的秒数",ts.TotalSeconds.ToString());
  148. i++;
  149. }
  150. }
  151. /// <summary> 
  152. /// 弹出信息并返回到指定页 
  153. /// </summary> 
  154. /// <param name="msg">弹出的消息</param> 
  155. /// <param name="url">指定转向的页面</param> 
  156. protected void Alert(string msg,string url)
  157. {
  158. string scriptString = "<script language=浏览>alert(/""+msg+"/");location.href=/""+url+"/"</script>";
  159. if(!this.IsStartupScriptRegistered("alert"))
  160. this.RegisterStartupScript("alert", scriptString);
  161. }
  162. /// <summary> 
  163. /// 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求 
  164. /// 这个方法也在下面的OnInit方法里调用 
  165. /// </summary> 
  166. protected void XmlReLoad()
  167. System.Text.StringBuilder htmlstr=new System.Text.StringBuilder();
  168. htmlstr.Append("<SCRIPT LANGUAGE=/"JavaScript/">");
  169. htmlstr.Append("function GetMessage(){");
  170. htmlstr.Append(" var xh=new ActiveXObject(/"Microsoft.XMLHTTP/");");
  171. htmlstr.Append(" xh.open(/"get/",window.location,false);");
  172. htmlstr.Append(" xh.send();");
  173. htmlstr.Append(" window.setTimeout(/"GetMessage()/",60000);");
  174. htmlstr.Append("}");
  175. htmlstr.Append("window.οnlοad=GetMessage();");
  176. htmlstr.Append("</SCRIPT> ");
  177. if(!this.IsStartupScriptRegistered("xmlreload"))
  178. this.RegisterStartupScript("alert", htmlstr.ToString());
  179. }
  180. override protected void OnInit(EventArgs e)
  181. {
  182. base.OnInit(e);
  183. this.PreventSessionTimeout();
  184. this.UpdateCacheTime();
  185. this.XmlReLoad();
  186. if(this.Cache["online"]!=null)
  187. {
  188. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  189. }
  190. }
  191. }
  192. }
  193. 五. 写一个自定义异常类
  194. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  195. using System;
  196. namespace oa.cls
  197. {
  198. /// <summary> 
  199. /// MyException 的摘要说明。 
  200. /// </summary> 
  201. public class MyException:ApplicationException
  202. {
  203. /// <summary> 
  204. /// 构造函数 
  205. /// </summary> 
  206. public MyException():base()
  207. {
  208. }
  209. /// <summary> 
  210. /// 构造函数 
  211. /// </summary> 
  212. /// <param name="ErrMessage">异常消息</param> 
  213. public MyException(string Message):base(Message)
  214. {
  215. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  216. }
  217. htmlstr.Append("window.οnlοad=GetMessage();");
  218. htmlstr.Append("</SCRIPT> ");
  219. if(!this.IsStartupScriptRegistered("xmlreload"))
  220. this.RegisterStartupScript("alert", htmlstr.ToString());
  221. }
  222. override protected void OnInit(EventArgs e)
  223. {
  224. base.OnInit(e);
  225. this.PreventSessionTimeout();
  226. this.UpdateCacheTime();
  227. this.XmlReLoad();
  228. if(this.Cache["online"]!=null)
  229. {
  230. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  231. }
  232. }
  233. }
  234. }
  235. 五. 写一个自定义异常类
  236. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  237. using System;
  238. namespace oa.cls
  239. {
  240. /// <summary> 
  241. /// MyException 的摘要说明。 
  242. /// </summary> 
  243. public class MyException:ApplicationException
  244. {
  245. /// <summary> 
  246. /// 构造函数 
  247. /// </summary> 
  248. public MyException():base()
  249. {
  250. }
  251. /// <summary> 
  252. /// 构造函数 
  253. /// </summary> 
  254. /// <param name="ErrMessage">异常消息</param> 
  255. public MyException(string Message):base(Message)
  256. {
  257. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  258. }
  259. htmlstr.Append("window.οnlοad=GetMessage();");
  260. htmlstr.Append("</SCRIPT> ");
  261. if(!this.IsStartupScriptRegistered("xmlreload"))
  262. this.RegisterStartupScript("alert", htmlstr.ToString());
  263. }
  264. override protected void OnInit(EventArgs e)
  265. {
  266. base.OnInit(e);
  267. this.PreventSessionTimeout();
  268. this.UpdateCacheTime();
  269. this.XmlReLoad();
  270. if(this.Cache["online"]!=null)
  271. {
  272. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  273. }
  274. }
  275. }
  276. }
  277. 五. 写一个自定义异常类
  278. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  279. using System;
  280. namespace oa.cls
  281. {
  282. /// <summary> 
  283. /// MyException 的摘要说明。 
  284. /// </summary> 
  285. public class MyException:ApplicationException
  286. {
  287. /// <summary> 
  288. /// 构造函数 
  289. /// </summary> 
  290. public MyException():base()
  291. {
  292. }
  293. /// <summary> 
  294. /// 构造函数 
  295. /// </summary> 
  296. /// <param name="ErrMessage">异常消息</param> 
  297. public MyException(string Message):base(Message)
  298. {
  299. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  300. }
  301. htmlstr.Append("window.οnlοad=GetMessage();");
  302. htmlstr.Append("</SCRIPT> ");
  303. if(!this.IsStartupScriptRegistered("xmlreload"))
  304. this.RegisterStartupScript("alert", htmlstr.ToString());
  305. }
  306. override protected void OnInit(EventArgs e)
  307. {
  308. base.OnInit(e);
  309. this.PreventSessionTimeout();
  310. this.UpdateCacheTime();
  311. this.XmlReLoad();
  312. if(this.Cache["online"]!=null)
  313. {
  314. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  315. }
  316. }
  317. }
  318. }
  319. 五. 写一个自定义异常类
  320. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  321. using System;
  322. namespace oa.cls
  323. {
  324. /// <summary> 
  325. /// MyException 的摘要说明。 
  326. /// </summary> 
  327. public class MyException:ApplicationException
  328. {
  329. /// <summary> 
  330. /// 构造函数 
  331. /// </summary> 
  332. public MyException():base()
  333. {
  334. }
  335. /// <summary> 
  336. /// 构造函数 
  337. /// </summary> 
  338. /// <param name="ErrMessage">异常消息</param> 
  339. public MyException(string Message):base(Message)
  340. {
  341. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  342. }
  343.  一. 设置web.config相关选项
  344. 先启用窗体身份验证和默认登陆页,如下。
  345. <authentication mode="Forms">
  346. <forms loginUrl="default.aspx"></forms>
  347. </authentication>
  348. 设置网浏览可以匿名访问,如下
  349. <authorization>
  350. <allow users="*" />
  351. </authorization>
  352. 然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
  353. <location path="admin">
  354. <system.web>
  355. <authorization>
  356. <deny users="?"></deny>
  357. </authorization>
  358. </system.web>
  359. </location>
  360. 把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
  361. <globalization requestEncoding="gb2312" responseEncoding="gb2312" />
  362. 设置session超时时间为1分钟,并启用cookieless,如下。
  363. <sessionState mode="InProc" cookieless="true" timeout="1" />
  364. 为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
  365. <trace enabled="true" requestLimit="1000" pageOutput="true" traceMode="SortByTime" localOnly="true" />
  366. 二. 设置Global.asax文件
  367. 处理Application_Start方法,实例化一个哈西表,然后保存在Cache里
  368. protected void Application_Start(Object sender, EventArgs e)
  369. {
  370. Hashtable h=new Hashtable();
  371. Context.Cache.Insert("online",h);
  372. }
  373. 在Session_End方法里调用LogoutCache()方法,方法源码如下
  374. /// <summary>
  375. /// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用 /// </summary>
  376. public void LogoutCache()
  377. {
  378. Hashtable h=(Hashtable)Context.Cache["online"];
  379. if(h!=null)
  380. {
  381. if(h[Session.SessionID]!=null)
  382. h.Remove(Session.SessionID);
  383. Context.Cache["online"]=h;
  384. }
  385. }
  386. 三. 设置相关的登陆和注销代码
  387. 登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
  388. /// <summary>
  389. /// 防止用户重复登陆,在用户将要身份验证前使用
  390. /// </summary>
  391. /// <param name="name">要验证的用户名字</param>
  392. private void PreventRepeatLogin(string name)
  393. {
  394. Hashtable h=(Hashtable)Cache["online"];
  395. if(h!=null)
  396. {
  397. IDictionaryEnumerator e1=h.GetEnumerator();
  398. bool flag=false;
  399. while(e1.MoveNext())
  400. if((string)((ArrayList)e1.Value)[0]==name)
  401. {
  402. flag=true;
  403. break;
  404. }
  405. }
  406. if(flag)
  407. {
  408. TimeSpan ts=System.DateTime.Now.Su浏览ract(Convert.ToDateTime(((ArrayList)e1.Value)[1]));
  409. if(ts.TotalSeconds<60)
  410. throw new oa.cls.MyException("对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!");
  411. else
  412. h.Remove(e1.Key); 
  413. }
  414. }
  415. else
  416. {
  417. h=new Hashtable();
  418. }
  419. ArrayList al=new ArrayList();
  420. al.Add(name);
  421. al.Add(System.DateTime.Now);
  422. h[Session.SessionID]=al;
  423. if(Cache["online"]==null)
  424. {
  425. Context.Cache.Insert("online",h);
  426. }else
  427. Cache["Online"]=h; 
  428. }
  429. 用户注销的时候调用上面提到LogoutCache()方法
  430. 四. 设置admin目录下的的所有页面的基类
  431. using System;
  432. using System.Web;
  433. using System.Web.UI;
  434. using System.Web.UI.WebControls;
  435. using System.Web.UI.HtmlControls;
  436. using System.Collections;
  437. namespace oa.cls
  438. {
  439. public class MyBasePage : System.Web.UI.Page
  440. {
  441. /// <summary>
  442. /// 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录
  443. /// </summary>
  444. protected bool IsAdminDir
  445. {
  446. get
  447. {
  448. return Request.FilePath.IndexOf("/oa/admin")==0;
  449. }
  450. }
  451. /// <summary>
  452. /// 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页
  453. /// </summary>
  454. private void PreventSessionTimeout()
  455. {
  456. if(!this.IsAdminDir) return;
  457. if(Session["User_Name"]==null&this.IsAdminDir)
  458. System.Web.Security.FormsAuthentication.SignOut();
  459. this.Alert("登陆超时",Request.ApplicationPath)
  460. }
  461. }
  462. /// <summary>
  463. /// 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用.
  464. /// </summary>
  465. private void UpdateCacheTime()
  466. {
  467. Hashtable h=(Hashtable)Cache["online"];
  468. if(h!=null)
  469. {
  470. ((ArrayList)h[Session.SessionID])[1]=DateTime.Now;
  471. }
  472. Cache["Online"]=h;
  473. }
  474. /// <summary>
  475. /// 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据
  476. /// </summary>
  477. /// <param name="myList"></param>
  478. private void TraceValues( Hashtable myList) 
  479. {
  480. IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
  481. int i=0;
  482. while ( myEnumerator.MoveNext() )
  483. {
  484. Context.Trace.Write( "onlineSessionID"+i, myEnumerator.Key.ToString());
  485. ArrayList al=(ArrayList)myEnumerator.Value;
  486. Context.Trace.Write( "onlineName"+i, al[0].ToString());
  487. Context.Trace.Write( "onlineTime"+i,al[1].ToString());
  488. TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(al[1].ToString()));
  489. Context.Trace.Write("当前的时间和此登陆时间间隔的秒数",ts.TotalSeconds.ToString());
  490. i++;
  491. }
  492. }
  493. /// <summary>
  494. /// 弹出信息并返回到指定页
  495. /// </summary>
  496. /// <param name="msg">弹出的消息</param>
  497. /// <param name="url">指定转向的页面</param>
  498. protected void Alert(string msg,string url)
  499. {
  500. string scriptString = "<script language=浏览>alert(/""+msg+"/");location.href=/""+url+"/"</script>";
  501. if(!this.IsStartupScriptRegistered("alert"))
  502. this.RegisterStartupScript("alert", scriptString);
  503. }
  504. /// <summary>
  505. /// 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求
  506. /// 这个方法也在下面的OnInit方法里调用
  507. /// </summary>
  508. protected void XmlReLoad()
  509. System.Text.StringBuilder htmlstr=new System.Text.StringBuilder();
  510. htmlstr.Append("<SCRIPT LANGUAGE=/"JavaScript/">");
  511. htmlstr.Append("function GetMessage(){");
  512. htmlstr.Append(" var xh=new ActiveXObject(/"Microsoft.XMLHTTP/");");
  513. htmlstr.Append(" xh.open(/"get/",window.location,false);");
  514. htmlstr.Append(" xh.send();");
  515. htmlstr.Append(" window.setTimeout(/"GetMessage()/",60000);");
  516. htmlstr.Append("}");
  517. htmlstr.Append("window.οnlοad=GetMessage();");
  518. htmlstr.Append("</SCRIPT> ");
  519. if(!this.IsStartupScriptRegistered("xmlreload"))
  520. this.RegisterStartupScript("alert", htmlstr.ToString());
  521. }
  522. override protected void OnInit(EventArgs e)
  523. {
  524. base.OnInit(e);
  525. this.PreventSessionTimeout();
  526. this.UpdateCacheTime();
  527. this.XmlReLoad();
  528. if(this.Cache["online"]!=null)
  529. {
  530. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  531. }
  532. }
  533. }
  534. }
  535. 五. 写一个自定义异常类
  536. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  537. using System;
  538. namespace oa.cls
  539. {
  540. /// <summary>
  541. /// MyException 的摘要说明。
  542. /// </summary>
  543. public class MyException:ApplicationException
  544. {
  545. /// <summary>
  546. /// 构造函数
  547. /// </summary>
  548. public MyException():base()
  549. {
  550. }
  551. /// <summary>
  552. /// 构造函数
  553. /// </summary>
  554. /// <param name="ErrMessage">异常消息</param>
  555. public MyException(string Message):base(Message)
  556. {
  557. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  558. }
  559. htmlstr.Append("window.οnlοad=GetMessage();");
  560. htmlstr.Append("</SCRIPT> ");
  561. if(!this.IsStartupScriptRegistered("xmlreload"))
  562. this.RegisterStartupScript("alert", htmlstr.ToString());
  563. }
  564. override protected void OnInit(EventArgs e)
  565. {
  566. base.OnInit(e);
  567. this.PreventSessionTimeout();
  568. this.UpdateCacheTime();
  569. this.XmlReLoad();
  570. if(this.Cache["online"]!=null)
  571. {
  572. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  573. }
  574. }
  575. }
  576. }
  577. 五. 写一个自定义异常类
  578. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  579. using System;
  580. namespace oa.cls
  581. {
  582. /// <summary>
  583. /// MyException 的摘要说明。
  584. /// </summary>
  585. public class MyException:ApplicationException
  586. {
  587. /// <summary>
  588. /// 构造函数
  589. /// </summary>
  590. public MyException():base()
  591. {
  592. }
  593. /// <summary>
  594. /// 构造函数
  595. /// </summary>
  596. /// <param name="ErrMessage">异常消息</param>
  597. public MyException(string Message):base(Message)
  598. {
  599. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  600. }
  601. htmlstr.Append("window.οnlοad=GetMessage();");
  602. htmlstr.Append("</SCRIPT> ");
  603. if(!this.IsStartupScriptRegistered("xmlreload"))
  604. this.RegisterStartupScript("alert", htmlstr.ToString());
  605. }
  606. override protected void OnInit(EventArgs e)
  607. {
  608. base.OnInit(e);
  609. this.PreventSessionTimeout();
  610. this.UpdateCacheTime();
  611. this.XmlReLoad();
  612. if(this.Cache["online"]!=null)
  613. {
  614. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  615. }
  616. }
  617. }
  618. }
  619. 五. 写一个自定义异常类
  620. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  621. using System;
  622. namespace oa.cls
  623. {
  624. /// <summary>
  625. /// MyException 的摘要说明。
  626. /// </summary>
  627. public class MyException:ApplicationException
  628. {
  629. /// <summary>
  630. /// 构造函数
  631. /// </summary>
  632. public MyException():base()
  633. {
  634. }
  635. /// <summary>
  636. /// 构造函数
  637. /// </summary>
  638. /// <param name="ErrMessage">异常消息</param>
  639. public MyException(string Message):base(Message)
  640. {
  641. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  642. }
  643. htmlstr.Append("window.οnlοad=GetMessage();");
  644. htmlstr.Append("</SCRIPT> ");
  645. if(!this.IsStartupScriptRegistered("xmlreload"))
  646. this.RegisterStartupScript("alert", htmlstr.ToString());
  647. }
  648. override protected void OnInit(EventArgs e)
  649. {
  650. base.OnInit(e);
  651. this.PreventSessionTimeout();
  652. this.UpdateCacheTime();
  653. this.XmlReLoad();
  654. if(this.Cache["online"]!=null)
  655. {
  656. this.TraceValues((System.Collections.Hashtable)Cache["online"]);
  657. }
  658. }
  659. }
  660. }
  661. 五. 写一个自定义异常类
  662. 首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
  663. using System;
  664. namespace oa.cls
  665. {
  666. /// <summary>
  667. /// MyException 的摘要说明。
  668. /// </summary>
  669. public class MyException:ApplicationException
  670. {
  671. /// <summary>
  672. /// 构造函数
  673. /// </summary>
  674. public MyException():base()
  675. {
  676. }
  677. /// <summary>
  678. /// 构造函数
  679. /// </summary>
  680. /// <param name="ErrMessage">异常消息</param>
  681. public MyException(string Message):base(Message)
  682. {
  683. System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
  684. }
  685. /// <summary>
  686. /// 构造函数
  687. /// </summary>
  688. /// <param name="Message">异常消息</param>
  689. /// <param name="InnerException">引起该异常的异常类</param>
  690. public MyException(string Message,Exception InnerException):base(Message,InnerException)
  691. {
  692. }
  693. }
  694. }
  695. 六.总结
  696. 我发现在Session里保存的值,比如session["name"]是没有任何向服务器的请求达到1分钟后就会自动丢失,但是session ID是关闭同一进程的浏览器页面后达1分钟后才会丢失并更换的,因为只要你开着浏览器就会有session ID,无论是在url里保存还是在cookies里。不知道这个结论对不对,反正我在设置了session的timeout为1分钟后,session["name"]的值已经没有了,可是SessionID还是旧的,Global.asax里的Session_End里的代码也没有执行,而身份验证票据也没有丢失。我不知道这三者之间的关系是怎样的,谁先谁后,好像在<authentication>小节可以设置一个timeout属性,不过项目赶的紧,我没时间研究了。
  697. 以上这些代码比较零散,我花费了2天的时间才总结出来这套方案,不是很完美,但是暂时只能这样了,不能在这方面浪费很多时间了,大家可以把上面的代码组织到一个类里,然后把方法都浏览改成静态方法方便调用。
  698. 最后大家有什么建议和改进的意见欢迎和我交流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值