会话超时后如何重定向到主页

本文关键字:重定向 主页 超时 会话 | 更新日期: 2023-09-27 17:58:46

我正在开发一个网站,我想在会话超时后重定向到主页。有人能帮我解决这个问题吗。

会话超时后如何重定向到主页

http://csharpdotnetfreak.blogspot.com/2008/11/detecting-session-timeout-and-redirect.html

这是ASP中检测会话超时并重定向到登录页的示例。NET中,当用户空闲了web.config文件中指定的时间时,会发生会话超时。

为此,我在web.config中将超时值设置为1分钟。

第一种方法

在web.config文件中,将会话状态模式设置为inproc,将身份验证模式设置为Forms

<system.web>
    <compilation debug="true"/>
    <authentication mode="Forms"/>
    <sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>
</system.web> 

在这个例子中,我创建了三个页面,一个是登录页面,当会话到期时,我重定向到这个页面,另一个是导航页面,我将在其中检查会话是否有效,如果有效,只有用户会看到这个页面,否则他会被重定向到登录页面。

在应用程序或网站的根目录中添加Global.asax类文件。此方法仅在应用程序中存在Global.asax的情况下有效。

在我们要检查会话超时的页面的Page_Init事件中写下下面提到的代码。

我们还可以将这些代码放入一个类中,并从该类继承应用程序的所有页面,作为所有页面的基类来检查会话超时。

C#代码

protected void Page_Init(object sender, EventArgs e)
{
    if (Context.Session != null)
    {
        if (Session.IsNewSession)
        {
            HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"];
            if (newSessionIdCookie != null)
            {
                string newSessionIdCookieValue = newSessionIdCookie.Value;
                if (newSessionIdCookieValue != string.Empty)
                {
                    // This means Session was timed Out and New Session was started
                    Response.Redirect("Login.aspx");
                }
            }
        }
    }
}

第二种方法

Default.aspx 代码

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC
          "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnSessionStart"
                        runat="server"
                        OnClick="btnSessionStart_Click"
                        Text="Start Session" /><br />
            <br />
            <br />
            <asp:Button ID="btnCheck"
                        runat="server"
                        OnClick="btnCheck_Click"
                        Text="Check Session ID" />
            <br />
            <asp:TextBox ID="txtSession"
                         runat="server"
                         Width="266px">
            </asp:TextBox><br />
            <br />
            <asp:Button ID="btnGO"
                        runat="server"
                        OnClick="btnGO_Click"
                        Text="Go to Other Page" />
            <br />
            <br />
        </div>
    </form>
</body>
</html>

这个页面后面的代码就像

protected void btnSessionStart_Click(object sender, EventArgs e)
{
    Guid Session_id = Guid.NewGuid();
    Session["SessionID"]
    = Session_id.ToString();
}
protected void btnCheck_Click(object sender, EventArgs e)
{
    if (Session["SessionID"] != null)
        txtSession.Text =
        Session["SessionID"].ToString();
    else
        txtSession.Text =
        "Session has expired";
}
protected void btnGO_Click(object sender, EventArgs e)
{
    Response.Redirect("Default2.aspx");
}

在我们想要检查会话是否超时的页面上,我们需要在页面的page_Init事件中进行检查,如果会话不为空,用户将能够转到该页面,否则他将被重定向到登录页面。

在这个页面中,我刚刚放了一个按钮转到主页

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnHome"
                        runat="server" OnClick="btnHome_Click"
                        Text="Home" />
        </div>
    </form>
</body>
</html>

这个页面后面的代码是

protected void Page_Init(object sender, EventArgs e)
{
    CheckSession();
}
protected void btnHome_Click(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx");
}
private void CheckSession()
{
    if (Session["SessionID"] == null)
    {
        Response.Redirect("Login.aspx");
    }
}

如果我们需要在应用程序的所有页面中检查这一点,那么我们可以创建一个BaseClass,并编写上面提到的CheckSession和Page_Init部分的代码,并通过键入BaseClassName代替System来驱动该类中的所有页面。网状物UI。页面,每次加载页面时,它都会检查所有页面的会话超时

来源:http://csharpdotnetfreak.blogspot.com/