已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.net解决了防止用户重复登陆,session超时等问题一设置web.config相关选项先启用窗体身份验证和默认登陆页,如下。设置网站可以匿名访问,如下然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。设置session超时时间为1分钟,并启用cookieless,如下。为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。二设置Global.asax文件处理Application_Start方法,实例化一个哈西表,然后保存在Cache里protected void Application_Start(Object sender, EventArgs e)Hashtable h=new Hashtable();Context.Cache.Insert(online,h);在Session_End方法里调用LogoutCache()方法,方法源码如下/ / 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用 / public void LogoutCache()Hashtable h=(Hashtable)Context.Cacheonline;if(h!=null)if(hSession.SessionID!=null)h.Remove(Session.SessionID);Context.Cacheonline=h;三设置相关的登陆和注销代码登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有 admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache online里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个 ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法./ / 防止用户重复登陆,在用户将要身份验证前使用/ / 要验证的用户名字private void PreventRepeatLogin(string name)Hashtable h=(Hashtable)Cacheonline;if(h!=null)IDictionaryEnumerator e1=h.GetEnumerator();bool flag=false;while(e1.MoveNext()if(string)(ArrayList)e1.Value)0=name)flag=true;break;if(flag)TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(ArrayList)e1.Value)1);if(ts.TotalSeconds60)throw new oa.cls.MyException(对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!);elseh.Remove(e1.Key);elseh=new Hashtable();ArrayList al=new ArrayList();al.Add(name);al.Add(System.DateTime.Now);hSession.SessionID=al;if(Cacheonline=null)Context.Cache.Insert(online,h);elseCacheOnline=h;用户注销的时候调用上面提到LogoutCache()方法四设置admin目录下的的所有页面的基类using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Collections;namespace oa.clspublic class MyBasePage : System.Web.UI.Page/ / 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录/ protected bool IsAdminDirgetreturn Request.FilePath.IndexOf(/oa/admin)=0;/ / 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页/ private void PreventSessionTimeout()if(!this.IsAdminDir) return;if(SessionUser_Name=null&this.IsAdminDir)System.Web.Security.FormsAuthentication.SignOut();this.Alert(登陆超时,Request.ApplicationPath)/ / 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用./ private void UpdateCacheTime()Hashtable h=(Hashtable)Cacheonline;if(h!=null)(ArrayList)hSession.SessionID)1=DateTime.Now;CacheOnline=h;/ / 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据/ / private void TraceValues( Hashtable myList)IDictionaryEnumerator myEnumerator = myList.GetEnumerator();int i=0;while ( myEnumerator.MoveNext() )Context.Trace.Write( onlineSessionID+i, myEnumerator.Key.ToString();ArrayList al=(ArrayList)myEnumerator.Value;Context.Trace.Write( onlineName+i, al0.ToString();Context.Trace.Write( onlineTime+i,al1.ToString();TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(al1.ToString();Context.Trace.Write(当前的时间和此登陆时间间隔的秒数,ts.TotalSeconds.ToString();i+;/ / 弹出信息并返回到指定页/ / 弹出的消息/ 指定转向的页面protected void Alert(string msg,string url)string scriptString = alert(+msg+);location.href=+url+;if(!this.IsStartupScriptRegistered(alert)this.RegisterStartupScript(alert, scriptString);/ / 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求/ 这个方法也在下面的OnInit方法里调用/ protected void XmlReLoad()System.Text.StringBuilder htmlstr=new System.Text.StringBuilder();htmlstr.Append();htmlstr.Append(function GetMessage();htmlstr.Append( var xh=new ActiveXObject(Microsoft.XMLHTTP););htmlstr.Append( xh.open(get,window.location,false););htmlstr.Append( xh.send(););htmlstr.Append( window.setTimeout(GetMessage(),60000););htmlstr.Append();htmlstr.Append(window.onload=GetMessage(););htmlstr.Append( );if(!this.IsStartupScriptRegistered(xmlreload)this.RegisterStartupScript(alert, htmlstr.ToString();override protected void OnInit(EventArgs e)base.OnInit(e);this.PreventSessionTimeout();this.UpdateCacheTime();this.XmlReLoad();if(this.Cacheonline!=null)this.TraceValues(System.Collections.Hashtable)Cacheonline);五 写一个自定义异常类首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。using System;namespace oa.cls/ / MyException 的摘要说明。/ public class MyException:ApplicationException/ / 构造函数/ public MyException():base()/ / 构造函数/ / 异常消息public MyException(string Message):base(Message)System.Web.HttpContext.Current.Response.Redirect(/ShowErr.aspx?msg=+Message);/ / 构造函数/ / 异常消息/ 引起该异常的异常类public MyException(string Message,Exception InnerException):base(Message,InnerException)六总结我发现在Session里保存的值,比如sessionname是没有任何向服务器的请求达到1分钟后就会自动丢失,但是 session ID是关闭同一进程的浏览器页面后达1分钟后才会丢失并更换的,因为只要你开着浏览器就会有session ID,无论是在url里保存还是在cookies里。不知道这个结论对不对,反正我在设置了session的t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论