【问题标题】:How to redirect to the previous page in asp.net after session expires会话过期后如何重定向到asp.net中的上一页
【发布时间】:2015-08-22 15:15:14
【问题描述】:

目前我正在做一个项目,当用户输入他的电子邮件和密码时,会创建一个登录会话 但是在会话过期一段时间后,会出现登录页面,当我输入详细信息时,我会被重定向到主页 有什么办法可以在会话过期之前回到上一页?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    很抱歉打扰您,但由于那里没有客户端,因此无法在 Session_End 重定向!会话结束时,您只会被重定向到目标 URL!

    不过,您可以重定向到登录!打开您的 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 页面,在 Login Button click 中编写如下代码:

     protected void Button1_Click(object sender, EventArgs e)   
     {   
    
         Session["LoginUserName"] = Convert.ToString(TextBox1.Text);   
         Response.Redirect("Welcome.aspx");   
      }  
    

    在上面的代码中,首先我们将登录用户名存储在 Session 中,以便我们可以在下一页获取登录用户名,然后我们将页面重定向到 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 页面,因此我们在页面加载时从 Session 获取当前用户登录名并分配给标签。

    我添加了另一个页面 AddUserDetails.aspx 并从上面的代码中调用它来确定会话是否已过期。如果它已过期,那么它将重定向到 AddUserDetails.aspx 页面,否则它会转到 Login.aspx 页面。

    现在我们的应用程序已准备好进行测试,让我们运行应用程序。将显示以下登录页面。

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多