ASP.NET页面'Page_Load'在Master Page的'Page_Load'事件之前触发?

时间:2022-05-26 15:52:35

On my Master Page, I have a little method in the Page_Load event that checks to see if a user is logged in, and redirects them to the Login page if not.

在我的母版页上,我在Page_Load事件中有一个小方法,用于检查用户是否已登录,如果没有,则将其重定向到“登录”页面。

The problem is that for some of my pages the Page_Load events presume a users logged are in, and these events seems to fire before the login check in the master page, which causes errors.

问题是,对于我的一些页面,Page_Load事件假定记录的用户都在,并且这些事件似乎在主页面中的登录检查之前触发,这会导致错误。

What are some ways around this? Any events I can use other than Page_Load in my pages, that'll fire after the master page?

有什么方法可以解决这个问题?我可以在我的页面中使用除Page_Load之外的任何事件,这些事件会在母版页之后触发吗?

3 个解决方案

#1


25  

You have a rich Page Cycle with lots of events to use. Perhaps you could use Page_Init to check if the user is logged-in in the Master Page. Or use Page_PreRender in the other pages.

您有一个丰富的Page Cycle,可以使用大量事件。也许您可以使用Page_Init来检查用户是否在主页面中登录。或者在其他页面中使用Page_PreRender。

#2


2  

If you need things to occur in the MasterPage Page_Load before the page events, use the Page_PreRender

如果您需要在页面事件之前在MasterPage Page_Load中发生事情,请使用Page_PreRender

protected void Page_PreRender(object sender, EventArgs e)

in the actual page.

在实际页面中。

#3


1  

You are going to have to check whether the user is logged in for those features, by doing: if (this.Page.User.Identity.IsAuthenticated == true) { .. }. Nothing can be assumed, which is what you are experiencing. You could also move your login check to Page_Init, or even move it to an HTTP module that runs on every page load; there you have access to a wide array of events including application authentication/authorization.

您将必须通过执行以下操作来检查用户是否已登录这些功能:if(this.Page.User.Identity.IsAuthenticated == true){..}。没有什么可以假设,这是你正在经历的。您还可以将登录检查移至Page_Init,甚至将其移至每个页面加载运行的HTTP模块;在那里,您可以访问各种事件,包括应用程序身份验证/授权。

If you are using forms authentication, you can use the configuration file to drive this instead, via the authorization element.

如果使用表单身份验证,则可以使用配置文件通过authorization元素来驱动它。

<system.web>
  <authorization>
    <deny users="?" />
    <allow users="*" />
  </authorization>
</system.web>
<location path="login.aspx">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

HTH.

HTH。

#1


25  

You have a rich Page Cycle with lots of events to use. Perhaps you could use Page_Init to check if the user is logged-in in the Master Page. Or use Page_PreRender in the other pages.

您有一个丰富的Page Cycle,可以使用大量事件。也许您可以使用Page_Init来检查用户是否在主页面中登录。或者在其他页面中使用Page_PreRender。

#2


2  

If you need things to occur in the MasterPage Page_Load before the page events, use the Page_PreRender

如果您需要在页面事件之前在MasterPage Page_Load中发生事情,请使用Page_PreRender

protected void Page_PreRender(object sender, EventArgs e)

in the actual page.

在实际页面中。

#3


1  

You are going to have to check whether the user is logged in for those features, by doing: if (this.Page.User.Identity.IsAuthenticated == true) { .. }. Nothing can be assumed, which is what you are experiencing. You could also move your login check to Page_Init, or even move it to an HTTP module that runs on every page load; there you have access to a wide array of events including application authentication/authorization.

您将必须通过执行以下操作来检查用户是否已登录这些功能:if(this.Page.User.Identity.IsAuthenticated == true){..}。没有什么可以假设,这是你正在经历的。您还可以将登录检查移至Page_Init,甚至将其移至每个页面加载运行的HTTP模块;在那里,您可以访问各种事件,包括应用程序身份验证/授权。

If you are using forms authentication, you can use the configuration file to drive this instead, via the authorization element.

如果使用表单身份验证,则可以使用配置文件通过authorization元素来驱动它。

<system.web>
  <authorization>
    <deny users="?" />
    <allow users="*" />
  </authorization>
</system.web>
<location path="login.aspx">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

HTH.

HTH。