Section 5.6. Create Accounts with CreateUserWizard

5.6. Create Accounts with CreateUserWizard

Note: Simplify user account creation with the new CreateUserWizard control.

Because creating user accounts is such as common task for most web sites, Microsoft has provided a new CreateUserWizard control in ASP.NET 2.0. The CreateUserWizard control takes the drudgery out of creating user accounts by providing a highly customizable control that accepts users' information. It performs such tasks as verifying users' passwords and authenticating email addresses. It then automatically adds the user account using the specified Membership Provider.

5.6.1. How do I do that?

In this lab, you will create a page to let users create an account on your web site. You will use the CreateUserWizard control to perform this task.

  1. Using the same project created in the last lab (C:\ASPNET20\chap-5-SecurityControls), add a new Web Form to the project (right-click the project name in Solution Explorer and then select Add New Item...; select Web Form). Name the Web Form NewUser.aspx.

  2. Drag and drop the CreateUserWizard control onto the form.

  3. Apply the Elegant scheme to the CreateUserWizard control (through the Auto Format... link in the CreateUserWizard Tasks menu).Your page should now look like the one shown in Figure 5-25.

    Figure 5-25. Using the CreateUserWizard control

    You can customize the look and feel of the CreateUserWizard control by using the CreateUserWizard Tasks menu. You can apply formatting templates, add a sidebar to it (via the DisplaySideBar property), or set the CreateUserWizard control to display the Cancel button (via the DisplayCancelButton property).

  4. Press F5 to test the application. Enter the information shown in Figure 5-26 and then click the Create User button.

    Figure 5-26. Creating a new user account

  5. If the account is created is successfully, you should see a notification like the one shown in Figure 5-27.

    Figure 5-27. Successful creation of a new user account

    Tip: You need to set the ContinueDestinationPageURL property of the CreateUserWizard control so that when the Continue button is clicked, the user can be redirected to another page.

  6. You can verify the correct format of the email address by specifying a regular expression for the CreateUserWizard control. This is set via the EmailRegularExpression property of the CreateUserWizard control.

5.6.2. What about...

...adding a user to your site programmatically?

Besides using the ASP.NET Web Site Administration Tool or CreateUserWizard control to add users to your web site, you can use the Membership class to add users programmatically to your web site. The Membership class allows you to perform common tasks involved in user management, such as user addition, deletion, and changing of passwords.

  1. Using the same project created in this lab (C:\ASPNET20\chap-5-SecurityControls), add a new Web Form to the project (right-click the project name in Solution Explorer and then select Add New Item...; select Web Form). Name the Web Form AddUser.aspx.

  2. Add a 2 6 table to the Web Form (Layout Insert Table) and populate it with the controls shown in Figure 5-28.

    Figure 5-28. Populating the Web Form

  3. Set the TextMode property of both the txtPassword1 and txtPassword2 controls to Password so that the password entered will not be displayed as plain text on the form.

  4. Double-click the Add User button to switch to code-behind, and type in the following code:

    Imports System.Web.Security Partial Class AddUser_aspx     Inherits System.Web.UI.Page     Protected Sub btnAddUser_Click(ByVal sender As Object, _                                    ByVal e As System.EventArgs) _                                    Handles btnAddUser.Click         Dim IsCreated As MembershipCreateStatus         If (txtPassword1.Text = txtPassword2.Text) Then             Membership.CreateUser(txtUserID.Text, txtPassword1.Text, _               txtEmail.Text, txtQn.Text, txtAns.Text, True, IsCreated)             Response.Write("Add user: " & IsCreated.ToString)         Else             Response.Write("Passwords do not match")         End If     End Sub End Class

  5. The MembershipCreateStatus enumeration returns the status of the user creation process. To test the application, press F5 and add a new user. The user information is saved in the aspnet_Users and aspMembership tables on the database specified by the Membership provider (by default, it is the SQL 2005 Express database).

    You can verify that the new user has been added by loading the Login.aspx form and logging in with the new user credential.

...deleting a user?

You can delete a user by using the DeleteUser( ) method in the Membership class:

Dim username as String Dim deleted as Boolean= _     Membership.DeleteUser(username)

The DeleteUser( ) method returns a Boolean result indicating whether the specified user is deleted successfully.

5.6.3. Where can I learn more?

To learn more about the various methods in the Membership class, check out the MSDN Help topic on "Membership Methods."

ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
Year: 2005
Pages: 104 © 2008-2017.
If you may any questions please contact us: