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

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

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 void ActionAddUser_Click(object sender, EventArgs e)

{

try

{

Membership.CreateUser(UserNameText.Text,

PasswordText.Text,

UserEmailText.Text);

StatusLabel.Text = “User created successfully!”;

}

catch(Exception ex)

{

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

StatusLabel.Text = “Unable to create user!”;

}

}

 

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.