How to use ASP.NET Membership class to create and delete users in the store in VB.NET

If you want to create users to your web site, you should follow the next steps:

C# in Depth, 3rd EditionManaged Windows VPS Hosting

1. Create a new page which contains the necessary text boxes for entering the required information.

2. Add a button

3. Handle the Click event of this button with the following code:


Protected Sub AddUser_Click(sender As Object, e As EventArgs) Handles AddUser.Click


Membership.CreateUser(UserNameText.Text, PasswordText.Text, UserEmailText.Text)

StatusLabel.Text = “User created successfully!”

Catch ex As Exception

Debug.WriteLine(“Exception: ” + ex.Message)

StatusLabel.Text = “Unable to create user!”

End Try

End Sub


The CreateUser method exists with several overloads. The easiest overload just accepts a user name and a password, while the more complex versions require a password question and answer as well.

Deleting users is as simple as creating users. The Membership class offers a DeleteUser() method that requires you to pass the user name as a parameter. It deletes the user as well as all related information, if you want, from the underlying membership store.