2

In my master page's Page_Load event, I have the following code:

if(!Request.IsAuthenticated)
{
   FormsAuthentication.RedirectToLoginPage();
} else 
{
   // Do something. Note: Any work here gets performed when request is authenticated.
}

However, when I load a content page (which uses the master) while not authenticated, I do not get redirected to the login page. To get the desired behavior, I also need to add the above check in the Page_Load event in my content page.

Not a huge issue but is just annoying having the above check in every content page.

So I am curious, what is the reason for the master page not redirecting to login page when request isn't authenticated?

bobble14988
  • 1,749
  • 5
  • 26
  • 38
  • possible duplicate of [ASP.net Masterpage\_load first or page\_load first?](http://stackoverflow.com/questions/4470196/asp-net-masterpage-load-first-or-page-load-first) – Curtis Yallop Feb 10 '15 at 17:55

5 Answers5

3

If all you want is FormsAuthentication to redirect to login page, when user has not logged in why don't you use config file to do this?

<system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

This should automatically send all 'anonymous' users to the configured login page for FormAuth.

BuddhiP
  • 6,231
  • 5
  • 36
  • 56
  • That, my friend, is very helpful :) the original question still stands though. – bobble14988 Dec 05 '12 at 16:46
  • Thank you! :) I'm also unable to figure out why that code doesn't work for you. I dont think its the order of the Load event, no matter which goes first, redirection should work. I think examples you have given are not sufficient to figure this out. – BuddhiP Dec 05 '12 at 16:54
  • This is a classic unhelpful SO answer-- technically correct, but not addressing the actual question! Setting this in web.config is global. A single site can have multiple Master pages, so objectively this isn't the answer to what is being asked here. See @Sagar's answer for the actual solution – Synctrex Dec 26 '18 at 18:03
2

Have you tried debugging this - is your masterpage's Page_Load definitely running? You might have an AutoEventWireUp set to false or similar...

user369142
  • 2,575
  • 1
  • 21
  • 9
  • My masterpage's Page_Load is definitely running, yes. Anything I do in the "else" block gets executed. So does any code outside the conditional. – bobble14988 Dec 05 '12 at 16:45
2

I think you need to see the life cycle of Master page; through this you can understand how it all works.

First the individual .aspx page's Page_Load() method is run and then, after execution of that method finishes, the master page's Page_Load() method is run.

Related question:What is the 'page lifecycle' of an ASP.NET WebForm?

Synctrex
  • 811
  • 1
  • 11
  • 19
Sagar Upadhyay
  • 819
  • 2
  • 11
  • 31
1

When you need the same functionality in several places, you can use ASP.NET User Controls.

Put your code in the usercontrol and add the usercontrol to the pages where you need this code to be executed

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
1

I dont know why your code doesn't work, it work perfect with me. You can check in your web.config, are you implement the url of login page

<authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" timeout="2880" defaultUrl="~/somepage.aspx" />
    </authentication>
Wolf
  • 6,361
  • 2
  • 28
  • 25