一.设置web.config相关选项
先启用窗体身份验证和默认登陆页,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">authentication </span><span style="color: #ff0000">mode</span><span style="color: #0000ff">="Forms"</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">forms </span><span style="color: #ff0000">loginUrl</span><span style="color: #0000ff">="default.aspx"</span><span style="color: #0000ff">></</span><span style="color: #800000">forms</span><span style="color: #0000ff">></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">authentication</span><span style="color: #0000ff">></span></div> |
设置网站可以匿名访问,如下
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">allow </span><span style="color: #ff0000">users</span><span style="color: #0000ff">="*"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span></div> |
然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">location </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="admin"</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">system.web</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">deny </span><span style="color: #ff0000">users</span><span style="color: #0000ff">="?"</span><span style="color: #0000ff">></</span><span style="color: #800000">deny</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"></</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"></</span><span style="color: #800000">system.web</span><span style="color: #0000ff">></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">location</span><span style="color: #0000ff">></span></div> |
把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">globalization </span><span style="color: #ff0000">requestEncoding</span><span style="color: #0000ff">="gb2312"</span><span style="color: #ff0000"> responseEncoding</span><span style="color: #0000ff">="gb2312"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
设置session超时时间为1分钟,并启用cookieless,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">sessionState </span><span style="color: #ff0000">mode</span><span style="color: #0000ff">="InProc"</span><span style="color: #ff0000"> cookieless</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> timeout</span><span style="color: #0000ff">="1"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">trace </span><span style="color: #ff0000">enabled</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> requestLimit</span><span style="color: #0000ff">="1000"</span><span style="color: #ff0000"> pageOutput</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> traceMode</span><span style="color: #0000ff">="SortByTime"</span><span style="color: #ff0000"> localOnly</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
二.设置Global.asax文件
处理Application_Start方法,实例化一个哈希表,然后保存在Cache里
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> Application_Start(Object sender, EventArgs e)<br />{<br /> Hashtable h</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> Hashtable();<br /> Context.Cache.Insert(</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">,h);<br />}</span></div> |
在Session_End方法里调用LogoutCache()方法,方法源码如下
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用<br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> LogoutCache()<br />{<br /> Hashtable h</span><span style="color: #000000">=</span><span style="color: #000000">(Hashtable)Context.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(h</span><span style="color: #000000">!=</span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(h[Session.SessionID]</span><span style="color: #000000">!=</span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> h.Remove(Session.SessionID);<br /> Context.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">h;<br /> }<br />}</span></div> |
三.设置相关的登陆和注销代码
登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> 防止用户重复登陆,在用户将要身份验证前使用<br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="name"></span><span style="color: #008000">要验证的用户名字</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> PreventRepeatLogin(</span><span style="color: #0000ff">string</span><span style="color: #000000"> name)<br />{<br /> Hashtable h </span><span style="color: #000000">=</span><span style="color: #000000"> (Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (h </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> IDictionaryEnumerator e1 </span><span style="color: #000000">=</span><span style="color: #000000"> h.GetEnumerator();<br /> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> flag </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">while</span><span style="color: #000000"> (e1.MoveNext())<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> ((</span><span style="color: #0000ff">string</span><span style="color: #000000">)((ArrayList)e1.Value)[</span><span style="color: #800080">0</span><span style="color: #000000">] </span><span style="color: #000000">==</span><span style="color: #000000"> name)<br /> {<br /> flag </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (flag)<br /> {<br /> TimeSpan ts </span><span style="color: #000000">=</span><span style="color: #000000"> System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[</span><span style="color: #800080">1</span><span style="color: #000000">]));<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (ts.TotalSeconds </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #800080">60</span><span style="color: #000000">)<br /> </span><span style="color: #0000ff">throw</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> oa.cls.MyException(</span><span style="color: #800000">"</span><span style="color: #800000">对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> h.Remove(e1.Key);<br /> }<br /> }<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> {<br /> h </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Hashtable();<br /> }<br /> ArrayList al </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> ArrayList();<br /> al.Add(name);<br /> al.Add(System.DateTime.Now);<br /> h[Session.SessionID] </span><span style="color: #000000">=</span><span style="color: #000000"> al;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> Context.Cache.Insert(</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">, h);<br /> }<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> Cache[</span><span style="color: #800000">"</span><span style="color: #800000">Online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> h;<br />}</span></div> |
用户注销的时候调用上面提到LogoutCache()方法
四.设置admin目录下的的所有页面的基类
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">using</span><span style="color: #000000"> System;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI.WebControls;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI.HtmlControls;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections;<br /></span><span style="color: #0000ff">namespace</span><span style="color: #000000"> oa.cls<br />{<br /> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> MyBasePage : System.Web.UI.Page<br /> {<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> IsAdminDir<br /> {<br /> </span><span style="color: #0000ff">get</span><span style="color: #000000"><br /> {<br /> </span><span style="color: #0000ff">return</span><span style="color: #000000"> Request.FilePath.IndexOf(</span><span style="color: #800000">"</span><span style="color: #800000">/oa/admin</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> PreventSessionTimeout()<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsAdminDir) </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(Session[</span><span style="color: #800000">"</span><span style="color: #800000">User_Name</span><span style="color: #800000">"</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #0000ff">null</span><span style="color: #000000">&&</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsAdminDir)<br /> { <br /> System.Web.Security.FormsAuthentication.SignOut();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.Alert(</span><span style="color: #800000">"</span><span style="color: #800000">登陆超时</span><span style="color: #800000">"</span><span style="color: #000000">,Request.ApplicationPath)<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用.<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> UpdateCacheTime()<br /> {<br /> Hashtable h </span><span style="color: #000000">=</span><span style="color: #000000"> (Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (h </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> ((ArrayList)h[Session.SessionID])[</span><span style="color: #800080">1</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> DateTime.Now;<br /> }<br /> Cache[</span><span style="color: #800000">"</span><span style="color: #800000">Online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> h;<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="myList"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> TraceValues(Hashtable myList)<br /> {<br /> IDictionaryEnumerator myEnumerator </span><span style="color: #000000">=</span><span style="color: #000000"> myList.GetEnumerator();<br /> </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">while</span><span style="color: #000000"> (myEnumerator.MoveNext())<br /> {<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineSessionID</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, myEnumerator.Key.ToString());<br /> ArrayList al </span><span style="color: #000000">=</span><span style="color: #000000"> (ArrayList)myEnumerator.Value;<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineName</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, al[</span><span style="color: #800080">0</span><span style="color: #000000">].ToString());<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineTime</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, al[</span><span style="color: #800080">1</span><span style="color: #000000">].ToString());<br /> TimeSpan ts </span><span style="color: #000000">=</span><span style="color: #000000"> System.DateTime.Now.Subtract(Convert.ToDateTime(al[</span><span style="color: #800080">1</span><span style="color: #000000">].ToString()));<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">当前的时间和此登陆时间间隔的秒数</span><span style="color: #800000">"</span><span style="color: #000000">, ts.TotalSeconds.ToString());<br /> i</span><span style="color: #000000">++</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 弹出信息并返回到指定页<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="msg"></span><span style="color: #008000">弹出的消息</span><span style="color: #808080"></param></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="url"></span><span style="color: #008000">指定转向的页面</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> Alert(</span><span style="color: #0000ff">string</span><span style="color: #000000"> msg, </span><span style="color: #0000ff">string</span><span style="color: #000000"> url)<br /> {<br /> </span><span style="color: #0000ff">string</span><span style="color: #000000"> scriptString </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"><script language=JavaScript>alert(\</span><span style="color: #800000">""</span><span style="color: #800000"> + msg + </span><span style="color: #800000">"</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000">);location.href=\</span><span style="color: #800000">""</span><span style="color: #800000"> + url + </span><span style="color: #800000">"</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000"></script></span><span style="color: #800000">"</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsStartupScriptRegistered(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">))<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.RegisterStartupScript(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">, scriptString);<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 这个方法也在下面的OnInit方法里调用<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> XmlReLoad()<br /> {<br /> System.Text.StringBuilder htmlstr </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> System.Text.StringBuilder();<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"><SCRIPT LANGUAGE=\</span><span style="color: #800000">"</span><span style="color: #000000">JavaScript\</span><span style="color: #800000">"</span><span style="color: #800000">></span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">function GetMessage(){</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> var xh=new ActiveXObject(\</span><span style="color: #800000">"</span><span style="color: #000000">Microsoft.XMLHTTP\</span><span style="color: #800000">"</span><span style="color: #800000">);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> xh.open(\</span><span style="color: #800000">"</span><span style="color: #0000ff">get</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000">,window.location,false);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> xh.send();</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> window.setTimeout(\</span><span style="color: #800000">"</span><span style="color: #000000">GetMessage()\</span><span style="color: #800000">"</span><span style="color: #800000">,60000);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">}</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">window.onload=GetMessage();</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"></SCRIPT> </span><span style="color: #800000">"</span><span style="color: #000000">);<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsStartupScriptRegistered(</span><span style="color: #800000">"</span><span style="color: #800000">xmlreload</span><span style="color: #800000">"</span><span style="color: #000000">))<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.RegisterStartupScript(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">, htmlstr.ToString());<br /> }<br /> </span><span style="color: #0000ff">override</span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> OnInit(EventArgs e)<br /> {<br /> </span><span style="color: #0000ff">base</span><span style="color: #000000">.OnInit(e);<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.PreventSessionTimeout();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.UpdateCacheTime();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.XmlReLoad();<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #0000ff">this</span><span style="color: #000000">.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.TraceValues((System.Collections.Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">]);<br /> }<br /> }<br /> }<br />}</span></div> |
五.写一个自定义异常类
首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">using</span><span style="color: #000000"> System;<br /></span><span style="color: #0000ff">namespace</span><span style="color: #000000"> oa.cls<br />{<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> MyException 的摘要说明。<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> MyException : ApplicationException<br /> {<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException()<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">()<br /> {<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="ErrMessage"></span><span style="color: #008000">异常消息</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException(</span><span style="color: #0000ff">string</span><span style="color: #000000"> Message)<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">(Message)<br /> {<br /> System.Web.HttpContext.Current.Response.Redirect(</span><span style="color: #800000">"</span><span style="color: #800000">~/ShowErr.aspx?msg=</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> Message);<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="Message"></span><span style="color: #008000">异常消息</span><span style="color: #808080"></param></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="InnerException"></span><span style="color: #008000">引起该异常的异常类</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException(</span><span style="color: #0000ff">string</span><span style="color: #000000"> Message, Exception InnerException)<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">(Message, InnerException)<br /> {<br /> }<br /> }<br />}</span></div> |
转自:http://www.cnblogs.com/hcbin/archive/2010/04/02/1702885.html