{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Saving the Custom User data from CreateUserWizard via Profiles
Summary
You have added the extra controls to the CreateUserWizard to collect additional information from the user at the time of new account creation. This article discusses how to save that extra information to the database.
 
Table of Contents

The CreatedUser Event

Using Profiles

Profile Properties

Listing 1. Profile Properties in the web.config file

ProfileCommon Class

Listing 2. The ProfileCommon class

Saving the Profiles

Listing 3. Saving the Profiles

Properties in the Database

Figure 1. Creating a new user

Figure 2. Profile Table

Figure 3. Database Diagram for Profiles

 

The default CreateUserWizard screens will let you obtain some information (like username, password, email, etc.) from the user and save it to the membership database. These are discussed in the following articles:

Extending Wizard Steps in CreateUserWizard

Authentication vs Authorization

Now, we want to store the newly created user related fields in the database as well. You would make use of two things in this regard:

  • The CreatedUser Event of the CreateUserWizard
  • Using Profiles to save the custom user information

The CreatedUser Event

The CreateUserWizard has a whole bunch of events; some are inherited from the Control (as every custom control does); some are inherited from the Wizard class (since Wizard is the immediate base class for CreateUserWizard); and then there are some CreateUserWizard specific events.

The specific events include user creation related events (CreatedUser, CreatingUser, etc.), sending mail related events (SendingMail, SendingMailError, etc.). The event of our interest is CreatedUser. This is the default event – if you double-click on the CreateUserWizard, this event handler will be created for you.

protected void cuwCreateUser_CreatedUser(object sender, EventArgs e)
{
}

Using Profiles

Profiles let you add additional pieces of information to a particular user. In this case, we want to store the first name, last name, and the company of the user. For this you need to do the following:

  • Define what pieces of information (properties of profile) in the web.config file.
  • Use the Profile related classes in the code to save and obtain these extra pieces of information about the user

Of course, here I am assuming that you have defined the membership database already.

Profile Properties

You would simply define the profile properties you want in web.config file.

Listing 1. Profile Properties in the web.config file

<profile enabled="true">
 <properties>
  <add name="FirstName" type="string"/>
  <add name="LastName" type="string"/>
  <add name="Company" type="string"/>
 </properties>
</profile>

The format of adding a property is straight-forward – specify the name and type of that property. Each of these properties can have additional attributes like defaultValue and allowAnonymous.

ProfileCommon Class

After you add the profile properties to the web.config file and compile the site, a ProfileCommon class will be generated for you. As you can see in Listing 2, this class has the profile properties as its own properties. You can get and set the FirstName, LastName, and Company we have defined in the web.config file.

Listing 2. The ProfileCommon class

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.1378
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.Profile;

public class ProfileCommon : System.Web.Profile.ProfileBase {
   
    public virtual string LastName {
        get {
            return ((string)(this.GetPropertyValue("LastName")));
        }
        set {
            this.SetPropertyValue("LastName", value);
        }
    }
   
    public virtual string Company {
        get {
            return ((string)(this.GetPropertyValue("Company")));
        }
        set {
            this.SetPropertyValue("Company", value);
        }
    }
   
    public virtual string FirstName {
        get {
            return ((string)(this.GetPropertyValue("FirstName")));
        }
        set {
            this.SetPropertyValue("FirstName", value);
        }
    }
   
    public virtual ProfileCommon GetProfile(string username) {
        return ((ProfileCommon)(ProfileBase.Create(username)));
    }
}

Saving the Profiles

When the Create User button in the CreateUserWizard is clicked, a user is created. And if the LoginCreatedUser property of this wizard is set to True, the newly created user is automatically logged in.

Once the user is created, the CreatedUser event is fired. The code used in Listing 3 is used to save the profiles that we used for this user.

Listing 3. Saving the Profiles

/// <summary>
/// Save the profile properties after the user has been created.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void cuwCreateUser_CreatedUser(object sender, EventArgs e)
{
    ProfileCommon userProfile = (ProfileCommon) ProfileCommon.Create(cuwCreateUser.UserName, true);

    // Update the profile values
    userProfile.FirstName = ((TextBox)cuwCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
    userProfile.LastName = ((TextBox)cuwCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
    userProfile.Company = ((TextBox)cuwCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("Company")).Text;

    // Save the values
    userProfile.Save();
}

In listing 3, we would first Create a profile using the UserName. The ProfileCommon class generated for us contains various properties (FirstName, LastName, etc.) that we need. And finally, using the Save() method, simply save these custom properties for the user.

Properties in the Database

Now let’s take a look at how profiles are saved in the database. First, I created a new user with the following values:

First Name: Sergey
Last Name: Brin
E-mail: sbrin@google.com
Company: Google
Etc.

Figure 1. Creating a new user

Creating a New User

The regular membership data (like username, password, etc.) are stored in the aspnet_Users, aspnet_Membership tables. And the profile data (first name, last name, etc.) are stored in the aspnet_Profile table.

Figure 2. Profile Table

Profile Table

The data in aspnet_Profile table is stored in an interesting way. All the profile properties are stored in the PropertyNames column and their values are stored in the PropertyValuesString.

PropertyNames: FirstName:S:0:6:LastName:S:6:4:Company:S:10:6:
PropertyValuesString: SergeyBrinGoogle

And the numbers next to the property names are begin and end characters in the PropertyValuesString.

Finally, let’s take a look at how aspnet_Profile is connected to other tables. As shown in Figure 3, the aspnet_Profile table is connected to aspnet_Users table via the UserId column. And of course, the aspnet_Users table is connected to a whole bunch of other tables like aspnet_UsersInRole, aspnet_Personalization, aspnet_Membership, and aspnet_Applications.

Figure 3. Database Diagram for Profiles

Database Diagram for Profiles

This article covered saving the profile information from the CreateUserWizard. A previous article (Extending Wizard Steps in CreateUserWizard) covered extending the user interface of CreateUserWizard. And finally, in (Adding a Role to a Newly Created User), we will take a look at adding a role to the user.

Bookmark and Share This

More Articles With Similar Tags
icon-roles-dbdiag.jpg
This article looks at adding a role to the user that has been newly created via the CreateUserWizard.
icon-CreateUserWizard-extended.jpg
This article discusses the CreateUserWizard and looks at extending the default user interface provided by it.
By default SQLEXPRESS is used as the database that contains the membership information. Some thoughts in this regard.
icon-aspnet_membership_dbdiag.jpg
This article discusses the difference between authentication and authorization. It also discusses how the data required to support each of these features is stored in the database in ASP.NET 2.0 sites.
About  Contact  Privacy Policy  Site Map