Click here to Skip to main content
Click here to Skip to main content

Web Profile Class Generator

By , 30 Jul 2008
 

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:

// 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)

About the Author

AlluvialDeposit
Software Developer (Senior)
Norway Norway
Member
Microsoft Certified Solutions Developer (MCSD)
 
My personal websites/projects:
CRM1.no - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralProfile Migration To Authenticated Usermembergokhanartuk21 Sep '08 - 9:45 
Hello!
Firstly I want to thank for your solution! I am also using a code same as you wrote!. But I didnt make a programme for that! Your solution is better!. My problem is I have anonymous users in my Profile Database! But When somebody login I want to save the anonymous profile as a logined profile! There was Profile_MigrateAnonymous in ASP.NET 2.0 Which is defined in Global.asax. But there is no directly access in Web application you know for use ProfileCommon or Profile. My question is How can retrieve anonymous profile as a logged profile. I hope I enough clear!
Thank you very much again for everything!
MY Best Regards!
Gokhan!
 
gokoli

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 30 Jul 2008
Article Copyright 2008 by AlluvialDeposit
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid