2015-08-22 47 views
0

後目前我工作在其中創建一個登錄會話,當用戶輸入他的電子郵件地址和密碼 但是當會話過期,然後一段時間後一個項目登錄頁面出現,當我輸入詳細信息,然後我被重定向到主頁 會話過期前有什麼方法可以回到上一頁?如何重定向到前一頁在asp.net會話過期

回答

0

對不起打破它給你,但沒有辦法在Session_End中重定向,因爲沒有客戶那裏!當會話結束時,您將被重定向到destinationurl!

您可以重定向到登錄,但!打開Web.config文件,會話超時設置爲1分鐘這樣的:

<system.web> 
    <sessionState mode="InProc" timeout="1"/> 
</system.web> 

現在打開Global.asax文件和寫的在session_start事件下面的代碼:

void Session_Start(object sender, EventArgs e)  
{ 
    // Code that runs when a new session is started 
    if (Session["LoginUserName"] != null) 
    { 
    //Redirect to Welcome Page if Session is not null 
     Response.Redirect("Welcome.aspx"); 

    } 
else 
    { 
    //Redirect to Login Page if Session is null & Expires  
     Response.Redirect("Login.aspx"); 

    } 

在如果會話爲空,則Global.asax文件的上面的代碼將被重定向到Login.aspx。如果沒有,則重定向到Welcome.aspx。我在Global.asax文件中做了這個邏輯,因爲Global.asax文件事件是全局觸發的。

現在整個Global.asax文件將作如下安排:

<%@ Application Language="C#" %> 
    <script RunAt="server"> 

    void Application_Start(object sender, EventArgs e) 
    { 
    // Code that runs on application startup 

} 

void Application_End(object sender, EventArgs e) 
{ 
    // Code that runs on application shutdown 

} 

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs 

} 

    void Session_Start(object sender, EventArgs e){ 

    // Code that runs when a new session is started 
    if (Session["LoginUserName"] != null) 
    { 
     //Redirect to Welcome Page if Session is not null 
     Response.Redirect("Welcome.aspx"); 

    } 
    else 
    { 
     //Redirect to Login Page if Session is null & Expires  
    Response.Redirect("Login.aspx"); 

    } 


} 

void Session_End(object sender, EventArgs e) 
{ 
    // Code that runs when a session ends.  
// Note: The Session_End event is raised only when the sessionstate mode 
    // is set to InProc in the Web.config file. If session mode is set to StateServer  
    // or SQLServer, the event is not raised. 

    } 

    </script> 

現在打開Login.aspx.cs頁,寫在登錄按鈕點擊下面的代碼:

protected void Button1_Click(object sender, EventArgs e) 
{ 

    Session["LoginUserName"] = Convert.ToString(TextBox1.Text); 
    Response.Redirect("Welcome.aspx"); 
    } 

在上面的代碼中,首先我們在會話中存儲登錄用戶名,以便我們可以在下一頁中獲得登錄用戶名,然後將頁面重定向到Welcome.aspx頁面。

現在寫在Welcome.aspx.cs頁面下面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
{ 
Label1.Text="WelCome " +Convert.ToString(Session["LoginUserName"]); 

} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("AddUserDetails.aspx"); 
} 
} 

在上面的代碼,在用戶登錄到應用程序被重定向到Welcome.aspx頁面,所以我們是後從會話中加載當前用戶登錄名並分配給標籤。

我已經添加了另一頁是AddUserDetails.aspx並把它稱爲從上面的代碼決定,如果會話已過期。如果它已過期,那麼它將重定向到AddUserDetails.aspx頁面,否則它將進入Login.aspx頁面。現在

我們的應用程序已經準備好進行測試,所以讓我們運行應用程序。將顯示以下登錄頁面。