判断Session已过期,并重定向到另一个网页。

这是我先前的做法.
             bool  isNewSession  =  HttpContext.Current.Session.IsNewSession;
            
string  logoutUrl  =  root  +   " home/logout " ;
            
if  (isNewSession)
            {
                     
//  if (HttpContext.Current.Session["sessionID"] == null) 这方法土了些,要创建一session变量.
                     JScript.AlertAndRedirect( " 登录已超时,请重新登录! " , logoutUrl);      
            }

可是不准确,搞不懂的是,我没有新开窗口,session没有过期的情况 isNewSession有时还会是True.
以下是参照:
http://blogs.msdn.com/psundars/archive/2007/11/07/how-do-i-detect-a-session-has-expired-and-redirect-it-to-another-page.aspx
后的代码:

bool  isNewSession  =  HttpContext.Current.Session.IsNewSession;
            
string  logoutUrl  =  root  +   " home/logout " ;
            
if  (isNewSession)
            {
                 
string  strCookieHeader  = HttpContext.Current.Request.Headers[ " Cookie " ];

                 
if  ( null   !=  strCookieHeader  &&  strCookieHeader.IndexOf( " ASP.NET_SessionId " >=   0 )
                 {
                     JScript.AlertAndRedirect(
" 登录已超时,请重新登录! " , logoutUrl);
                 }
            }



-----------------------------------------------------
关于session和cookie的关系
Session是可以依赖Cookie的,意思就是您也可以不使用Cookie来保存SessionID, 关于如何在无Cookie 会话状态下Session的使用您可以参考MSDN: http://msdn.microsoft.com/zh-cn/library/system.web.sessionstate.httpsessionstate.sessionid(VS.80).aspx.

当使用Cookie来保存这个SessionID时,之所以找不到相应的Cookie,是因为它是保存在内存当中的。

转载于:https://www.cnblogs.com/solo/archive/2008/08/05/1260768.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值