Using Profile Information on a Base Page in ASP.NET

Recently I was attempting to code a base page in an ASP.NET project because I decided it would make future upkeep easy, and it was good practice to learn. What I wanted to do actually was set a page’s theme based on an entry in their profile, PreferredTheme. The thing is, I kept getting slammed by System.Configuration.SettingsPropertyNotFoundException errors and for the life of me couldn’t find much good information on the web. After piecing together things here and there, I’ve figured it out and thought I’d share it here in case somebody else has the same problem.

The simplest profile setting in a web.config file would look something like this:

Note: only authenticated users can have this profile information. If you want anonymous users to be provided profiles, use the allowAnonymous option in the profile property, and set anonymousIdentification enabled to “true”.

What I was trying to do in my base page was simply set the Page.Theme property in the Page_PreInit method like so:

Click to Enlarge

But that doesn’t even compile, so I followed some leads from Google around and found this page telling me to actually use the ProfileCommon() class to create a new instance of ProfileCommon, and then use the GetUserProfile() method to get the authenticated user’s profile, then finally get the information I needed out of the profile. Sounds great, right? Wrong! It may have compiled, but it sure didn’t run! I kept getting the SettingsPropertyNotFoundException, and I couldn’t quite pinpoint why.

Well, according to MSDN,

“When your application runs, ASP.NET creates a ProfileCommon class, which is a dynamically generated class that inherits the ProfileBase class. The dynamic ProfileCommon class includes properties created from the profile property definitions you specify in your application configuration. An instance of this dynamic ProfileCommon class is then set as the value of the Profile property of the current HttpContext and is available to pages in your application.”

After some thought (and a little help from a StackOverfow answer suggesting I go through the HttpContext to get Profile), I think figured out what was happening.

The dynamically created ProfileCommon class seems to be standing in for the authenticated user’s Profile information, so when I try calling Profile.PreferredTheme, I get that error because PreferredTheme simply doesn’t exist in our dynamic class. Instead, the more proper way is how the kind soul at SO stated: HttpContext.Current.Profile, which properly points to the authenticated user’s profile information.

After a little modification of this bit of code, I ended up with this new Page_PreInit function, which works just dandy:

Click to Enlarge

So there you have it: how to properly get Profile information from an authenticated user from your Base Page in ASP.NET. I hope this helps somebody out there.

Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: