Introduction
Creating a website with membership feature is easy in ASP.NET. The Membership feature of ASP.NET is easy to install (using aspnet_regsql) and just as easy to use in your code. This article will not cover how to install and use it. If you want to learn more about ASP.NET's Membership feature, you should take a look at the article: Multipart Series on ASP.NET's Membership, Roles, and Profile from 4GuysFromRolla.
You will have to set up custom properties in your web.config file like this:
<profile>
<properties>
<add name="PostalCode" />
</properties>
</profile>
You can even group your properties like this:
<profile>
<properties>
<group name="Personal">
<add name="FirstName" type="System.String" />
<add name="LastName" type="System.String" />
<add name="Email" type="System.String" />
<add name="BirthDate" type="System.DateTime" />
</group>
<group name="Address">
<add name="Street" type="System.String" />
<add name="ZipCode" type="System.String" />
<add name="City" type="System.String" />
</group>
</properties>
</profile>
To access these properties, you can use the GetPropertyValue(string propertyName)
and SetPropertyValue(string propertyName, object propertyValue)
functions found in System.Web.HttpContext.Current.Profile
. It would have been easier if these properties were accessible through a class. In a website it is, but not in a web application, so you will have to code this class manually. This simple program will generate this class for you. All you have to do is select which web.config file you will use, and the program will do the rest.
The program is very easy, and uses XmlDocument
and XPath to navigate your web.config file. It loops through your profile-section and generate a class with easy access to the profile properties. For each group in the properties, it generates a class, and inside that class, it generates properties for easy access to GetPropertyValue()
and SetPropertyValue()
. The program also creates a Save()
method for saving changes in the user profile.
The web.config example above would give us this code:
public static class UserProfile
{
public class Personal
{
public static System.String FirstName
{
get { return HttpContext.Current.Profile.GetPropertyValue("FirstName")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
}
public static System.String LastName
{
get { return HttpContext.Current.Profile.GetPropertyValue("LastName")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
}
public static System.String Email
{
get { return HttpContext.Current.Profile.GetPropertyValue("Email")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
}
public static System.DateTime BirthDate
{
get { return (System.DateTime)HttpContext.Current.Profile.
GetPropertyValue("BirthDate"); }
set { HttpContext.Current.Profile.SetPropertyValue("BirthDate", value); }
}
}
public class Address
{
public static System.String Street
{
get { return HttpContext.Current.Profile.GetPropertyValue("Street")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("Street", value); }
}
public static System.String ZipCode
{
get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
}
public static System.String City
{
get { return HttpContext.Current.Profile.GetPropertyValue("City")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
}
}
public static void Save()
{
HttpContext.Current.Profile.Save();
}
}
To make it even more easy, you can add the built-in properties in the same class:
public static bool IsAuthenticated
{
get { return HttpContext.Current.User.Identity.IsAuthenticated; }
}
public static Guid UserId
{
get { return (Guid)Membership.GetUser().ProviderUserKey; }
}
public static string Email
{
get { return Membership.GetUser().Email; }
}
public static DateTime DateCreated
{
get { return Membership.GetUser().CreationDate; }
}
public static string UserName
{
get { return Membership.GetUser().UserName; }
}
Using this class is easy. This example will show you how to read properties and change properties:
string firstname = UserProfile.Personal.FirstName;
UserProfile.Personal.FirstName = "Bill";
UserProfile.Personal.LastName = "Gates";
UserProfile.Save();
Points of Interest