2015-09-25 55 views
0

我有這樣的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); 
      } 
     } 
    } 
} 

當我直接去管理頁面時,會爲空就會去到登錄頁面。但是當我使用用戶名和密碼登錄時,系統會給我一條消息「瀏覽器有一個重定向循環」。當我按下瀏覽器後退按鈕時,應該使我的AdminPage保持在同一頁面上的過程是什麼,它只會進入LoginPage,會話爲空或會話由註銷事件處理程序終止。或者,如果用戶按下了網絡瀏覽器返回按鈕,系統將通過是或否的決定提醒用戶「您要註銷」。如果沒有系統將保持在同一頁面上,如果是,則會話將被終止並且系統重定向到登錄頁面。

我的登錄頁面代碼:

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>"); 
      } 
    } 
} 
+1

您是否已經在管理員頁面上?在這種情況下,'else'子句可能會不斷地重定向。嘗試不使用'else' ... –

+0

我嘗試過,沒有其他功能先生,但系統會將頁面重定向到登錄頁面,但會話仍處於活動狀態。只希望我的系統在會話終止時只登錄登錄頁面。 –

+0

我編輯了我的帖子先生。 –

回答

3

根據這行代碼:public partial class AdministratorPage : System.Web.UI.Page,我想你是在Admin Page

讓我們想象一下,你去Admin Page當你Session["adminlogin"] NOT NULL,則病情會去else部分

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

,是它讓你返回到Admin Page。這是循環的原因。 解決方法應該是:刪除else部分。

只是希望我的系統只去了loginpage當會話終止

這意味着您防止用戶在Login Page來當Session仍然存在。解決方案是:檢查Login Page中的Session,然後根據需要重定向到另一個頁面。

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

謝謝你的幫助! –

+0

很高興聽到:) – AnhTriet

2

該系統是給消息「瀏覽器有重定向循環」,因爲這部分代碼的。

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

只要登錄成功要重定向的頁面到同一個頁面(AdministratorPage.aspx),這是導致重定向循環。 刪除else部分。

+0

http://stackoverflow.com/questions/32786544/how-to-stay-on-the-same-page-until-session-expires/32786686#comment53412055_32786544 –

+0

@laurencekeithalbano你確定'Session [「adminlogin」]'成功登錄後不爲空? –

+0

是的先生,我編輯了我的問題我添加了管理員功能 –

0

這是我對我的上述問題,正確的代碼:

登錄頁面:

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); 
      } 
     } 
    } 
} 

有了這個,即使按網頁瀏覽器後退按鈕,它會重定向到他們當前的頁面。