zoukankan      html  css  js  c++  java
  • 判断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,是因为它是保存在内存当中的。

  • 相关阅读:
    DRF JWT认证基础
    Shell简单脚本
    DockerFile案例
    Shell基础知识
    DockerFile基础知识
    tensorflow
    使用cv2将图片分割成相等的部分
    序列化心得
    python正则化表达式
    python--匿名函数(lambda)
  • 原文地址:https://www.cnblogs.com/solo/p/1260768.html
Copyright © 2011-2022 走看看