ASP.NET Profiles in Web Application Projects
Accessing ASP.NET profiles from a web application project is remarkably easy once you know how. Here’s how it’s done, along with a fully working implementation that you can download.
Earlier this year, I blogged about how to create a strongly-typed Profile that could be accessed from a class library. In response to popular demand, today I’m presenting a similar (but more substantial) example geared specifically toward accessing profiles in web application projects.
There are plenty of blog posts that talk about this, but unfortunately very few that show how to do it. Most of them instead refer to this relatively obscure add-in for Visual Studio 2005 (apparently. you’re out of luck if you’re using Visual Studio 2008). However, the good news is you don’t need a third party add-in or build task for this, as implementing this functionality yourself is not difficult at all.
If you’re reading this, I’ll assume you’re aware of the differences between an ASP.NET web site and a web application, so I won’t go into the various differences in great detail (feel free to consult the MSDN for more information). For our purposes, it’s just important to understand that in a web application project, there is no ProfileCommon class or Page.Profile property to work with, as we’re accustomed to seeing in web site projects – hence the problem.
Web site projects accomplish this bit of magic through dynamic compilation – essentially creating the needed objects at runtime, “on the fly” as it were. In effect we will be doing the exact same thing, but the old-fashioned way – with a simple hand-written proxy class.
Writing a Profile Proxy Class
Let’s assume for the sake of discussion that we have several bits of user information we want to store in profile.
- first name
- last name
- date of birth
- gender
- the user’s time zone
- the user’s preferred culture
Now, we could store these as separate value types, but in practice I find it easier to group related properties together in a class. So let’s create a class Personal to store the first four properties, and a class Preferences to store the time zone and culture. Make sure to decorate these classes with the [Serializable] attribute.
namespace LD.Sample.BLL { using System; [Serializable] public class Personal { public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public Gender Gender { get; set; } } }
namespace LD.Sample.BLL { using System; [Serializable] public class Preferences { public string TimeZone { get; set; } public string Culture { get; set; } } }
Next, we’ll write a class that will define the profile we want, incorporating the two custom types shown above as properties. This class will inherit ProfileBase . Let’s call it CustomProfile.
namespace LD.Sample.BLL { using System.Web; using System.Web.Profile; public class CustomProfile : ProfileBase { public Personal Personal { get { return (Personal) GetPropertyValue("Personal"); } } public Preferences Preferences { get { return (Preferences) GetPropertyValue("Preferences"); } } ////// Get the profile of the currently logged-on user. /// public static CustomProfile GetProfile() { return (CustomProfile) HttpContext.Current.Profile; } ////// Gets the profile of a specific user. /// /// The user name of the user whose profile you want to retrieve. public static CustomProfile GetProfile(string userName) { return (CustomProfile) Create(userName); } } }
Note that I’ve also included two overloaded static GetProfile methods. GetProfile() lets us obtain the profile of the current user, while GetProfile(string userName) lets us get the profile of a specific user (for administrative purposes, or when creating a new member, for example).
One more thing – and this is very important – we need to define the provider in web.config.
You’ll see that our provider is of type System.Web.Profile.SqlProfileProvider. Of course, this assumes that the database to which this is pointing has been configured with the schema required to support SqlProfileProvider.
Also note the inherits attribute in
Usage
And that, believe it or not, is all there is to setting up a strongly-typed profile for use in a web application project. Now, let’s take a look at how you can use this newly-implemented goodness.
Here is some code that shows setting profile values for the currently logged-on user.
CustomProfile profile = CustomProfile.GetProfile(); profile.Personal.FirstName = "Tami"; profile.Personal.LastName = "Gharst"; profile.Personal.DateOfBirth = new DateTime(1972, 7, 10); profile.Personal.Gender = Gender.Female; profile.Preferences.TimeZone = "W. Europe Standard Time"; profile.Preferences.Culture = "de-DE"; profile.Save();
Here is some code that shows setting profile values for a specific user with the user name “Lee”.
CustomProfile profile = CustomProfile.GetProfile("Lee"); profile.Personal.FirstName = "Lee"; profile.Personal.LastName = "Dumond"; profile.Personal.DateOfBirth = new DateTime(1961, 10, 9); profile.Personal.Gender = Gender.Male; profile.Preferences.TimeZone = "Central Standard Time"; profile.Preferences.Culture = "en-US"; profile.Save();
If you like, you can make this even simpler by adding a Profile property to the page you’re working with – or even better, inherit your pages from a base type and add it there, so it’s available to all of your pages. This lets you set and retrieve profile properties with the familiar ASP.NET web site syntax.
// set profile properties Profile.Personal.FirstName = "Lee"; Profile.Personal.LastName = "Dumond"; Profile.Personal.DateOfBirth = new DateTime(1961, 10, 9); Profile.Personal.Gender = Gender.Male; Profile.Preferences.TimeZone = "Central Standard Time"; Profile.Preferences.Culture = "en-US"; Profile.Save();
// display profile properties lblDisplay.Text = string.Format( "User {0} had a real name of {1} {2}, was born on {3}, lives in the {4} time zone, and has a preferred culture of {5}.", Profile.UserName, Profile.Personal.FirstName, Profile.Personal.LastName, Profile.Personal.DateOfBirth.ToShortDateString(), Profile.Preferences.TimeZone, Profile.Preferences.Culture);
Sample Application
As I mentioned, I’ve included a downloadable web application solution that demonstrates web application profiles in action. When you run it, you’ll be greeted by the following screen:
There are screens that let you display a profile, edit a profile, and create a new one.
Examining the source code, along with the preceding explanation, should make understanding these concepts a snap.
I hope this helps the next time you find yourself needing access to profile properties from a web application!
Download sample code: WebApplicationProfileSample.zip
Subscribe to this blog for more cool content like this!
Pingback from Getting Strongly Typed Profile Properties From a Class Library : LeeDumond.com
Pingback from Twitter Trackbacks for ASP.NET Profiles in Web Application Projects : LeeDumond.com [leedumond.com] on Topsy.com
You've been kicked (a good thing) - Trackback from DotNetKicks.com
DotNetBurner - burning hot .net content
Pingback from ASP.NET Profiles in Web Application Projects : LeeDumond.com | Webmaster Tools
Pingback from Dew Drop – October 9, 2009 | Alvin Ashcraft's Morning Dew
Daily tech links for .net and related technologies - October 9-11, 2009 Web Development Using MvcContrib
Thank you for submitting this cool story - Trackback from DotNetShoutout
You are voted (great) - Trackback from WebDevVote.com