如何在会话过期之前保持在同一页面上?

How to stay on the same page until session expires?

我这里有这个 aspx 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MSSQLConnector;
using System.Data;
using System.Data.SqlClient;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class AdministratorPage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //If user run this page without logging in, it will redirect the user to the login page
            if (Session["adminlogin"] == null)
            {
               Response.Redirect("LoginPage.aspx", true);
            }
        }
    }
}

当我在会话为空时直接进入管理员页面时,它将进入登录页面。但是当我使用用户名和密码按登录时,系统会给我一条消息"That the browser has a Redirect Loop"。当我按下浏览器后退按钮时,我应该采取什么流程使我的 AdminPage 保持在同一页面上,它只会转到 LoginPage 是会话为空或会话被注销事件处理程序终止。或者如果可能的话,如果用户按下网络浏览器的后退按钮,系统将提醒用户 "Do you want to log out" 是或否的决定。如果否,系统将停留在同一页面上,如果是,会话将终止,系统将重定向到登录页面。

我的登录页面代码:

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MSSQLConnector;
using System.Data;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class OnlineAppSyss : System.Web.UI.Page
    {
        public void Admin()
        {
            //String decleration
            string adminusername = (this.UserName.Value);
            string adminpass = (this.Password.Value);

            try
            {
                if (adminusername == "admin" && adminpass == "cmpe1234")
                {
                    Session["adminlogin"] = adminusername;
                    Response.Redirect("AdministratorPage.aspx");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }
    }
}

由于您代码中的这一部分,系统给出了一条消息 "That the browser has a Redirect Loop"。

  else
  {
       Response.Redirect("AdministratorPage.aspx", true);
  }

只要您的登录成功,您就会将页面重定向到导致重定向循环的同一页面 (AdministratorPage.aspx)。 删除else部分。

基于这行代码:public partial class AdministratorPage : System.Web.UI.Page,我想你在 Admin Page.

让我们想象一下,当您的 Session["adminlogin"] NOT NULL 转到 Admin Page 时,那么 条件将转到 else 部分

    if (Session["adminlogin"] == null)
    {
       Response.Redirect("LoginPage.aspx", true);
    }
    else
    {
        Response.Redirect("AdministratorPage.aspx", true);
    }

它会不断将您返回到 Admin Page。这就是循环的原因。 解决方法应该是:去掉else部分。

Just want my system to only go the loginpage when session is terminated

这意味着当 Session 仍然存在时,您可以阻止用户进入 Login Page。解决方案是:检查 Login Page 中的 Session,然后根据需要重定向到另一个页面。

// Check for session existence
if (Session["adminlogin"] != null)
{
    // Redirect to another page
    Response.Redirect("AdministratorPage.aspx", true);
}

这是我针对上述问题的正确代码:

对于登录页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MSSQLConnector;
using System.Data;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class LoginPage: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //If session is not null  redirect to this page
            if (Session["adminlogin"] != null)
            {
                Response.Redirect("AdministratorPage.aspx", true);
            }
        }
        public void Admin()
        {
            //String decleration
            string adminusername = (this.UserName.Value);
            string adminpass = (this.Password.Value);

            try
            {
                if (adminusername == "admin" && adminpass == "cmpe1234")
                {
                    Session["adminlogin"] = adminusername;
                    Response.Redirect("AdministratorPage.aspx");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }
        }
    }
}

对于管理页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MSSQLConnector;
using System.Data;
using System.Data.SqlClient;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class AdministratorPage : System.Web.UI.Page
    {


       protected void Page_Load(object sender, EventArgs e)
        {
            //If session is null, go to login page
            if (Session["adminlogin"] == null)
            {
                Response.Redirect("LoginPage.aspx", true);
            }
        }
    }
}

即使按下网络浏览器的后退按钮,它也会重定向到他们的当前页面。