【问题标题】:asp.net on session timeout redirect to home pageasp.net 上的会话超时重定向到主页
【发布时间】:2011-08-02 02:16:32
【问题描述】:

我有网络应用程序和页面上的会话超时和用户交互,这需要重定向到主页/登陆页面

网上找到的解决办法

1) 应用程序的所有 aspx 页面的 page_load 中的会话检查。 2) global.asax 的会话开始中的代码

public void Session_Start    
{
        Response.Redirect("home.aspx");
        // or Server.Transfer("home.aspx");
}

我要选择第二个选项,请告诉我 1)我是否以正确的方式或任何更好的解决方案? 2)在第二个选项中是使用Response.Redirect还是Server.Transfer

-谢谢

【问题讨论】:

    标签: asp.net session-timeout response.redirect global-asax server.transfer


    【解决方案1】:

    我会选择第一个并检查会话.....

    在母版页的 OnInit 方法中编写以下代码将轻松完成您的任务

        /// <summary>
        /// Check for the session time out 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Context.Session != null)
            {
                //check whether a new session was generated
                if (Session.IsNewSession)
                {
                    //check whether a cookies had already been associated with this request
                    HttpCookie sessionCookie = Request.Cookies["ASP.NET_SessionId"];
                    if (sessionCookie != null)
                    {
                        string sessionValue = sessionCookie.Value;
                        if (!string.IsNullOrEmpty(sessionValue))
                        {
                            // we have session timeout condition!
                            Response.Redirect("Home.aps");
                        }
                    }
                }
            }
        } 
    

    【讨论】:

      【解决方案2】:

      你为什么不使用 JavaScript 来做呢?您可以使用setTimeout 方法,如

      <script type="text/javascript">
      setTimeout('window.location = "home.aspx"', 3000);
      </script>
      

      将上面的js代码块放入页眉中,3000是你的会话超时时间。

      【讨论】:

      • 您能否告诉我与第二种解决方案相比的好处(我已经提到过)。在第二个中,只是 global.asax 文件中的一行代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2012-09-12
      • 2016-11-12
      相关资源
      最近更新 更多