ASP.NET 2.0 allows for a very robust user authorization/authentication system with little to no code. One of the added features of the ASP.NET Membership and Profile systems is the ability to have custom user profiles. The ASP.NET Profile system allows you to add additional user profile fields very quickly. I will discuss in this post the methods needed to add simple profile fields (Standard system types), however remember that you can use complex objects for profile fields as well.
The process of adding custom profile fields to user accounts is broken into the following steps, which will be discussed in order in this posting.
- Add Profile section to Web.Config to identify the fields to add
- Add additional fields to the CreateUserWizard to capture the extra data at account creation
- Retrieve and display the values
Add profile section to the web.config to identify profile fields
Prior to adding the Profile section to your web application the assumption is made that you currently have your application setup correctly to use ASP.NET Authorization and that the database connectivity is correct. For our example we will add fields for "first name", "last name", and "address" a very common activity for web applications. Below is the settings needed to add the profile fields, below the example I will explain what is involved
This section should be added within the element of your configuration file. This adds the needed fields to your application. Note in this example all elements are created as strings, you can create elements as other types by adding the type property. This is all that is needed to setup the profile objects for use.
Add additional fields to the CreateUserWizard
To capture your additional profile fields you can convert the CreateUserWizard step to a TemplateStep then add your fields to the control at your leisure. I will not show the example of my CreateUserWizard control as the code is too lengthy, however you can use ANY control you desire simply add it to the page as you would on any other ASP.NET page.
Once you have the controls added to the page you will need to handle the "CreatedUser" event to ensure that your additional information is saved. I decided to use the "CreatedUser" event so I could be sure that the user account already existed. Below is the code needed to retreive and save the profile information, I will explain this code later.
//Get the profile of the user by finding the profile by name
TextBox oUserName = (TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
ProfileCommon oProfile = Profile.GetProfile(oUserName.Text);
//Set the values to be stored, note the use of find control
oProfile.FirstName = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtFirstName")).Text;
oProfile.LastName = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtLastName")).Text;
oProfile.Address = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtAddress")).Text;
oProfile.City = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtCity")).Text;
oProfile.State = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtState")).Text;
oProfile.Zip = ((TextBox)createUser.CreateUserStep.ContentTemplateContainer.FindControl("txtZip")).Text;
//Save the profile
This section is really very basic code you can see how easy ASP.NET 2.0 makes it to save the information. The process is a simple three part process, the only thing to note is that since your controls are hosted within the TemplateStep you are unable to directly reference them from the codebehind. You must find the control, then cast it back to a control of its type. In my example the objects for input were all TextBoxes.
Retrieve and display the values
Now that you have this information stored you want to retrieve it and display it to the user, say to provide a custom welcome page that displays their name. To retrieve the information you simply do the following.
lblName.text = Profile.FirstName + " " + Profile.LastName
ASP.NET automatically retrieves and populates the "Profile" object with the values for the particular user so you access the values just like you would any other property.
This has provided a very quick introduction to using the profile object to populate and retrieve custom user profile data. Please let me know if there are any questions.