[英]ASP.NET page 'Page_Load' firing before Master Page's 'Page_Load' event?
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之外的任何事件,这些事件会在母版页之后触发吗?
You have a rich Page Cycle with lots of events to use. 您有一个丰富的Page Cycle ,需要使用大量事件。 Perhaps you could use Page_Init
to check if the user is logged-in in the Master Page. 也许您可以使用Page_Init
来检查用户是否在主页面中登录。 Or use Page_PreRender
in the other pages. 或者在其他页面中使用Page_PreRender
。
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. 在实际页面中。
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) { .. }
. 您将必须通过执行以下操作来检查用户是否已登录这些功能: 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; 您还可以将登录检查移至Page_Init,甚至将其移至每个页面加载运行的HTTP模块; there you have access to a wide array of events including application authentication/authorization. 在那里,您可以访问各种事件,包括应用程序身份验证/授权。
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.