asp.net在会话超时重定向到主页

Yog*_*esh 1 asp.net response.redirect session-timeout server.transfer global-asax

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

在网上找到的解决方案

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

public void Session_Start    
{
        Response.Redirect("home.aspx");
        // or Server.Transfer("home.aspx");
}
Run Code Online (Sandbox Code Playgroud)

我要去第二个选项,让我知道1)我是否正确或更好的解决方案?2)在第二个选项中是否使用Response.Redirect或Server.Transfer

-谢谢

Pra*_*ana 5

我会去第一个并检查会议.....

在Master页面的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");
                    }
                }
            }
        }
    } 
Run Code Online (Sandbox Code Playgroud)