This instruction is for the programmer that doesn’t really care to hear about the nuts and bolts of adding profile properties to membership users, but just wants the quick read on what to do. I’m assuming that you’re already using the ASP.NET 2.0 membership services.
Adding custom properties for users is really quite simple. In the web.config you define your properties in the <system.web> section like the following code. <profile> <properties> <add name="FirstName"/> <add name="LastName"/> <add name="Address1"/> <add name="Address2"/> <add name="City"/> <add name="State"/> <add name="Zip" /> <add name="PhoneNumber"/> </properties> </profile>
Once you have defined your properties in the web.config, the properties magically become accessible through the ProfileCommon class. The ProfileCommon class is only available after adding the Profile section to the web.config.
Now that you have defined the additional properties for users, you can now set and retrieve these properties.
You would probably want to set the properties when creating the user or editing a user’s information. For this purpose you could use code similar to the following. Notice you need to call the Save after you set the properties.
Dim userProfile As ProfileCommon = ProfileCommon.Create(CreateUserWizard1.UserName, True)
With userProfile .FirstName = txtFirstName.Text.Trim .LastName = txtLastName.Text.Trim .Address1 = txtAddress1.Text.Trim .Address2 = txtAddress2.Text.Trim .City = txtCity.Text.Trim .State = drpState.SelectedItem.Value .Zip = txtZip.Text.Trim .PhoneNumber = txtPhoneNumber.Text.Trim .Save() End With
You can then retrieve information for a user using code similar to the following.
Dim userProfile As ProfileCommon = ProfileCommon.Create(HttpContext.Current.User.Identity.Name, True)
Dim strFirstName As String = userProfile.FirstName Dim strLastName As String = userProfile.LastName Dim strCompany As String = userProfile.Company Dim strAddress1 As String = userProfile.Address1 Dim strAddress2 As String = userProfile.Address2 Dim strCity As String = userProfile.City Dim strState As String = userProfile.State Dim strZip As String = userProfile.Zip Dim strPhoneNumber As String = userProfile.PhoneNumber
You could then display this information on an administration page.
That’s all there is to it. For a working example including a page to manage user information, you can register and download my User Management example project.
|