Click here to Skip to main content
15,879,239 members
Articles / Web Development / ASP.NET
Article

Web Profile Class Generator

Rate me:
Please Sign up or sign in to vote.
3.86/5 (8 votes)
30 Jul 2008CPOL2 min read 34.4K   482   22   1
This program will generate a class with easy access to custom properties used in the ASP.NET membership feature.

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:

XML
<profile>
  <properties>
    <add name="PostalCode" />
  </properties>
</profile>

You can even group your properties like this:

XML
<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:

C#
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:

C#
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:

C#
// read properties
string firstname = UserProfile.Personal.FirstName;

// write properties
UserProfile.Personal.FirstName = "Bill";
UserProfile.Personal.LastName = "Gates";

// save changes
UserProfile.Save();

Points of Interest

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Aurum AS
Norway Norway
Microsoft Certified Solutions Developer (MCSD)

Personal website:
stian.net

My projects:
CRM1.no - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software
Timeføring.no - A free to use norwegian timereg software
MittUtlegg - A free to use norwegian software for receipts
SupportWeb - A free to use norwegian software customersupport

Comments and Discussions

 
GeneralProfile Migration To Authenticated User Pin
gokhanartuk21-Sep-08 9:45
gokhanartuk21-Sep-08 9:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.