判断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/p/1260768.html