ASP.NET Page Inheritance flow?

G

Guest

Hi,

I want to create a base Webform where I want to put code to check if the
logged on user is allowed to view the page.

I want to put this code in once place and have all secured webforms inherit
from this base class

What I would like to happen is that the code in the base class be called
first (maybe in the Page_Load, but I'm not sure) to do the checking. My code
is below. The default flow for asp.net seems that any base class events are
called *after* descendent event runs. One way I got my base class code to
fire was by calling a custom funtion called CheckPermissions() as listed
below at the beginning of the page_load....

I guess what I wanted to avoid was having to explicitly put the
this.CheckPermissions in every descendant page. It would seem to me that if
a class is inherits another class the base class code would be called first
but I haven't done this type of thing yet so I'm not sure.

Base page
------------

public class WebFormBase : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string pageName = Page.ToString();
pageName = pageName.Replace("ASP.", "").Replace ("_", ".");

//--Code to check if the current logged on user has rights to view the
value of pageName from the permissions table.
}

protected void CheckPermissions()
{
string pageName = Page.ToString();
pageName = pageName.Replace("ASP.", "").Replace("_", ".");
//--Code to check if the current logged on user has rights to view the
value of pageName from the permissions table.


}

}



Descendent Page inherits WebFormBase..
----------------------------------------------
public class Home : WebFormBase
{
protected void Page_Load(object sender, System.EventArgs e)
{
this.CheckPermissions(); //check if user can view page.
...
'--proceed to load page...

'--while debugging, WebFormBase.Page_Load() is called now? How to make
this run at the beginning of this event so I don't have to call
CheckPermissions explicitly?
}
}
 
B

Brock Allen

I want to create a base Webform where I want to put code to check if
the logged on user is allowed to view the page.

Sorry this doesn't answer your question directly, but you don't need to do
this. ASP.NET provides automatic authorization checks for you based upon
the identity of the user or any roles that s/he is in. Check out:

http://msdn.microsoft.com/library/d...uide/html/cpconSimpleCookieAuthentication.asp

Even if this doesn't satisify your needs, you don't need to necessarily put
this check in a base class. You can put the check in global.asax in the Application_AuthorizeRequest
event. You can then do whatever checks are necessary and call Response.Redirect()
and/or Application.EndRequest() to fail the call such that the page the user
is trying to access won't be seen.
 
G

Guest

Thanks Brock! I forgot about the AuthorizeRequest event. Just to satisfy my
curiosity, if you have any links related my original post that would be
helpful too!
Dave
 
B

Brock Allen

Thanks Brock! I forgot about the AuthorizeRequest event. Just to
satisfy my curiosity, if you have any links related my original post
that would be helpful too! Dave

Umm, not off the top of my head. One way to insure the order is to instead
override OnLoad() instead of handling the Page_Load event. So then in your
OnLoad() you do whatever you need first, then call base.OnLoad() if your
authorization checks succeed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top