Problem referencing IPrincipal from my base page class...

M

Mike Taylor

I'm trying to create a base page class for my project (am new to
ASP.NET, so am a bit stumped) - I'm using VS.NET 2003, btw.

OK, my dummy base page class is shown below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Security.Principal;

namespace NewBarn.WebModules
{
public class ltAdminPage2 : System.Web.UI.Page
{
private IPrincipal p = HttpContext.Current.User;

public ltAdminPage2()
{
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

}
}


In this project, I'm also implementing custom security, and in this
base page class, I want to determine the current user's roles etc. by
accessing the HttpContext.Current.User.

However, this line seems to be tripping things up for some reason:
private IPrincipal p = HttpContext.Current.User;

If I try and create a .aspx page and change the code-behind to inherit
from ltAdminPage2 rather than System.Web.UI.Page, VS.NET won't let me
open that page in design view.

When I try to open the .aspx in VS.NET, I get an error: "The file
could not be loaded into the Web Forms designer. Please correct the
following error and then try loading it again: An exception occurred
while trying to create an instance of NewBarn.WebModules.ltAdminPage2.
The exception was 'Object reference not set to an instance of an
object.'. Make sure all of the classes used in the page are built or
referenced in the project."

If I comment out the IPrincipal line, it opens the file just fine (but
obviously I can't access the current user's Principal).

What have I done wrong? Am I not referencing the IPrincipal properly?

Confused.

Mike Taylor
 
B

bruce barker

when you page is opened in design mode, HttpContext is null, so the
HttpContext.Current.User reference thows an error. you need to add code to
detect that the page is in design mode (there is no builtin property, so its
common to test HttpContext) and not use any feature that will not work.

-- bruce (sqlwork.com)
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top