ASP.NET关于Forms验证关闭窗口自动注销的解决办法

mac2022-06-30  23

从MSDN和网上大多数资料来看,自动注销时触发Session_End事件有2中方式,一种是Session.Abandon(),另外一种是关闭所有窗口等待Session的timeout过期。前一种是可靠的,不过需要用户手动去执行Logout操作,后者则不那么可靠了,timeout过了基本上就没动静了,随便你怎么等,有人说是Bug,这个我就没有深入研究了。

那么如何实现关闭窗口就自动登出呢,这里有几个地方要注意。 1、在login的时候用到FormsAuthenticationTicket类,参照MSDN的写法:

CodeDim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _                                                                                             strUserID, _                                                                                             DateTime.Now, _                                                                                             DateTime.Now.AddMinutes(1), _                                                                                             True, _                                                                                             strUserData, _                                                                                             FormsAuthentication.FormsCookiePath)                     ' Encrypt the ticket.                    Dim encTicket As String = FormsAuthentication.Encrypt(ticket)                     ' Create the cookie.                    Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, encTicket))                     Response.Redirect(FormsAuthentication.GetRedirectUrl(strUserID, True))

这里手动将登陆信息写入Cookie中,稍微麻烦一点,注意这里我的timeout时间是1分钟哈。这样下次访问页面的时候会自动转到登录窗口,而不管你上次时候成功。

2、新建一个EndSession.aspx页面,禁用页面缓存,并且里面只有一行代码Session.Abandon(),这个我就不仔细说了。

3、在你的主页面或者Master页面里面加入以下javascript:

Code <script language="javascript" type="text/javascript">     function window.onunload()        {            if(event.clientX < 0 && event.clientY < 0//mouse is out of work space        EndSession();       }           function EndSession()     {         CreatexmlHttpRequest();         xmlHttp.open("GET","<%=Session("ServerBaseURL")%>/Security/EndSession.aspx?Math='" + Math.random() + "'",true);         xmlHttp.send(null);     }          function CreatexmlHttpRequest()     {         xmlHttp=false;         try         {             xmlHttp=new xmlHttpRequest();         }         catch(e)         {             var xmlHttpVersions = new Array("MSXML2.xmlHttp.6.0",                                             "MSXML2.xmlHttp.5.0",                                             "MSXML2.xmlHttp.4.0",                                             "MSXML2.xmlHttp.3.0",                                             "MSXML2.xmlHttp",                                             "Microsoft.xmlHttp");             for(var i=0;i<xmlHttpVersions.length&&!xmlHttp;i++)             {                 xmlHttp=new ActiveXObject(xmlHttpVersions[i]);             }         }         if(!xmlHttp)             alert("Error Creating the xmlHttpRequest Object.");         else             return xmlHttp;     }</script>

 

 

这些就是我从搜集的资料里汇总出的一个方法,也许不适合你,权作抛砖引玉。

注意这里的Session(“ServerBaseURL”)是网站的Root URL,后面加了一个随机数来防止Cache动作。

 

转载于:https://www.cnblogs.com/LazzyTiger/archive/2009/01/22/1379937.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)