1

It's an odd one. My master page Page_load seems to run AFTER the pages Page_load. Basically I'm trying to catch if a user is logged in / session is still active and if not, redirect them to the login page. But it falls over because it hits the individual pages page_load. I don't really want to have to put the check on each page.

Master page:

public partial class Cafe : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
               // lblName.Text = (string)Session["FirstName"];
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }


        }
    }
}

When I put a breakpoint in, the one on the page (take default.aspx for example) gets hit first, then the one on the master page

Loofer
  • 6,841
  • 9
  • 61
  • 102
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

3 Answers3

2

That is the normal page lifecycle. The page is not contained in the master page - the master page is contained in the page. It get processed between the page events and the events for the other controls on the page.

From the MSDN site

  1. Content page PreInit event.
  2. Master page controls Init event.
  3. Content controls Init event.
  4. Master page Init event.
  5. Content page Init event.
  6. Content page Load event.
  7. Master page Load event.
  8. Master page controls Load event.
  9. Content page controls Load event.
  10. Content page PreRender event.
  11. Master page PreRender event.
  12. Master page controls PreRender event.
  13. Content page controls PreRender event.
  14. Master page controls Unload event.
  15. Content page controls Unload event.
  16. Master page Unload event.
  17. Content page Unload event.

You can use the Global.asax Application_AuthenticateRequest event to authenticate the user. This will be called before any of the page processing.

Gridly
  • 938
  • 11
  • 13
2

Think of a Master page as just a fancy web control. If I were you, I'd have all my web pages inherit from a single base page and define your redirect in there instead.

public partial class MyPage : BasePage
{

}

public class BasePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("~/login.aspx");
        }
    }
}

Edit: I posted this before I saw why you were redirecting. Why aren't you using the built in ASP.NET functionality for the login by modifying your web.config to do the redirect for you?

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH">
    </forms>
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>
adam0101
  • 29,096
  • 21
  • 96
  • 174
  • Thanks for that. What do I need to put in the login page for that to work? It redirects okay, but wont let me login – MissCoder87 Aug 11 '14 at 16:16
  • 1
    After you authenticate the user, are you calling something like `FormsAuthentication.RedirectFromLoginPage(UserEmail.Text, Persist.Checked)` to create the authentication ticket and save it to a cookie? You shouldn't need to put anything special in your login page. The user should just be able to login again the same way they did the first time. Check this page out for a simple example of implementing Forms Authentication: http://msdn.microsoft.com/en-us/library/vstudio/xdt4thhy(v=vs.100).aspx – adam0101 Aug 11 '14 at 18:49
1

The Page_Load event always run in each page before running the master page. This is the most detailed listing I found on Page event order.

Session data is available in the Init event. You could move the logic to the master page Init event if you want it to execute before all page Page_Load events.

Travis Wolfe
  • 146
  • 1
  • 6