I have created a BasePage class that inherits from System.Web.Ui.Page. In that base class I have a bool property that checks to see if a page is secure or not. Initially, I put the code in the PreInit event (of the base class), but after thinking about it, my derived pages will not be able to set the bool value before PreInit. I then thought of setting the value in PreInit of the dervcied pages and checking that value in PageInit of the base class, but what if I need to use the PreInit in the derived page?
I thought about using partial methods, but I don’t think I can do that because the page events are not partials in System.Web.Ui.Page, right?
My BasePage class is an abstract class, by the way.
This is what I have now (I have not tested this, but assumed it may work):
public abstract partial class BasePage: System.Web.UI.Page
{
public bool IsSecure { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
if (!IsSecure) return;
if (PageMaster == null)
return;
if (!PageMaster.IsUserLoggedIn)
{
HttpContext.Current.Response.Redirect("~/WebForms/LogIn.aspx");
}
}
}
public partial class _Default : BasePage
{
protected void Page_PreInit(object sender, EventArgs e)
{
IsSecure = true;
}
}
A better solution may be to override the OnInit method in your base class. You can now still handle the init event in your pages, with the secure check carried out before the event is raised.
so: