Test the Extended Membership API
I've implemented the event handler for the Update User button as shown below:
Now that you have developed the ExtendedMemberShipProvider, I'll look at a set of examples on how to use this provider.
Before you can take the next step, you need to configure the newly developed provider in the web.config
file of the web application. The Provider Toolkit utility creates the configuration sections needed to add to the web.config
file (see Listing 4
file configures the current web application to use the new ExtendedMemberShipProvider.
|Figure 2. CreateUserWizard: This figure shows the CreateUserWizard with three custom fields.|
Now that you have configured the new provider in the web.config
file, you can start testing that provider. All the samples that I've presented are part of a web application in the accompanying downloadable code of this article.
The first sample creates a new user as shown in the ASPX page shown in Figure 2
. The figure shows the CreateUserWizard that you customized to have three additional fields: FirstName
, and DateOfBirth
. By default, the CreateUserWizard calls the CreateUser
method in the Membership Provider; however, since I'm using a different provider, I will keep the default behavior of that control the same, and then I can override the event called OnCreatedUser
, where I'll add my custom fields to the UserInfo table.
Once the CreateUserWizard finishes adding the member records to the Membership-related tables, ASP.NET raises the OnCreatedUser
event to process the custom fields. Listing 5
shows the implementation of that event.
The CreateUserWizard1_CreatedUser method gets the custom-field values from the CreateUserWizard control, checks if ASP.NET actually created the member records in the database, and then adds the detailed information about that member to the UserInfo
table. The second sample deletes a user from the database.
shows a drop-down list with all the user names found in the database. When you select a user name, you need to press the Delete button to completely delete the member record and its details from the UserInfo
|Figure 3. Delete User Screen: Here's how the Delete User screen looks in the sample application.|
I've implemented the event handler for the Delete button in the following code snippet:
protected void btnDelete_Click(object sender,
// Get the selected user name:
string UserName =
this.lblMsg.Text = "User deleted";
this.lblMsg.Text = "User could not be deleted!";
The last sample that I'll discuss is the Update User screen (see Figure 4
). The UI displays the member's user name, Bilal, from the drop-down list. Once you select the user name, you need to click the Submit button. After that ASP.NET displays a populated form with the chosen member's information below the drop-down list. You can now edit those fields and click "Update User" to update that user in the database.
|Figure 4. Update User Screen: Here's how the Update User screen looks in the sample application.|
The method above gets the field values from the form, and then calls the ExtendedMembership's
protected void updatebtn_Click(object sender, EventArgs e)
// Update the user
string Email = Server.HtmlEncode(this.Email.Text);
DateTime DateOfBirth = Convert.ToDateTime(
string Comments = Server.HtmlEncode(this.Comment.Text);
bool isApproved = this.IsApproved.SelectedValue.Equals(
"1")? true : false;
// Execute Update
MembershipUser _mu = Membership.GetUser(
_mu.Email = Email;
_mu.Comment = Comments;
_mu.IsApproved = isApproved;
_mu, null, null, DateOfBirth));
this.ErrorMessage.Text = "User record update successfully!";
this.ErrorMessage.Text = "User record could not be updated!";
this.Panel2.Visible = false;