【问题标题】:Previous Next ASP.NET application and Session Time Out RedirectionPrevious Next ASP.NET 应用程序和会话超时重定向
【发布时间】:2011-03-24 13:25:45
【问题描述】:

我有一个应用程序,我试图在会话超时时重定向,因此在我的母版页中,我正在检查重定向的会话变量是否为空,但问题是我有其他页面(派生)来自母版页和派生页面的 Page_Load 我也在那里引用了一些会话变量,我观察到派生页面(内容)的 PAGE_LOAD 事件首先触发,母版页 PAGE_LOAD 稍后触发,因此错误弹出“未设置对象引用”。

顺便说一句,我正在 LOGIN_BUTTON_PRESSED EVENT 上编写以下代码。

FormsAuthenticationTicket ticket - new FormsAuthenticationTicket(1, userName, DateTime.Now, DataTime.Now.AddMinutes(20), true, myRoles, FormsAuthentication.FormsCookiePath);

Session["uid"] = userName.Text;  
Session["ufullname"] = ufname;  

string hashCookies = FormsAuthentication.Encrypt(ticket);  

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);    

Response.Cookies.Add(cookie);  

Response.Redirect("~/Main.aspx");

请通过示例提出解决此问题的解决方案。

【问题讨论】:

    标签: asp.net session session-cookies


    【解决方案1】:

    一种选择是将母版页 Page_Load 事件中的代码移动到 Page_Init 事件:

    protected void Page_Init( object sender, EventArgs e )
    {
        //Your code goes here
    }
    

    ASP.NET Page Life Cycle Overview

    【讨论】:

    • Tchami 你把我的问题搞错了。我粘贴上面的代码告诉我正在使用 FormsAuthentication。我正在检查 MasterPage (Page_Load) 事件上的会话变量,因此它也可以继承到内容页面,但内容 (Page_Load) 首先触发,因此对象引用错误正在弹出。现在你们有什么建议。
    • 我想我没有。 Page_Init 事件将在 Page_Load 事件之前触发,因此,如果您将母版页 Page_Load 事件中的代码移动到母版页中的 Page_Init 事件,您的内容页面上应该不会出现任何对象引用错误。
    • 好吧,让我解释一下……在我的 MasterPage.master.cs 文件和 Page_Load 事件中,我正在编写 if (Session["uid"] == null || Session["ufullname"] == null ) { Response.Redirect("~/Default.aspx");在内容页面“MyProfile.aspx.cs”Page_Load 事件中我正在编写。 ((Label)Master.FindControl("Label1")).Text = "我的个人资料"; ((Label)Master.FindControl("ufname")).Text = "欢迎," + Session["ufullname"]; lbl_usertext.Text = Session["ufullname"].ToString();根据您的建议,我已将母版页代码移到 Page_Init 事件中,但仍然出现对象引用错误。
    • 这一行 Session["ufullname"].ToString();
    • 实际上,当我让应用程序空闲 20 分钟并单击我的任何内容页面链接时,page_loads 会触发具有相同行的页面。
    猜你喜欢
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2011-09-23
    • 2011-08-02
    相关资源
    最近更新 更多