In the previous post I showed You how to manage roles for forms authentication. Now I’ll explain how to manage users from the code side.

The main class which lets You manage users from provider is System.WebSecurity.SqlMembershipProvider class.

To get the SqlMembershipProvider object You have to use System.Web.Security.Membership class and pass the name of the provider You want to obtain:


SqlMembershipProvider provider = Membership.Providers["ProviderName"] as SqlMembershipProvider;

To get the list of all existing users in the provider:


MembershipUserCollection users = provider.GetAllUsers();

To add a new user:


MembershipCreateStatus createStatus;
provider.CreateUser("UserName", "UserPassword", "UserEmail", "passwordQuestion", "passwordAnswer", true, Guid.NewGuid(), out createStatus);

To delete user:


provider.DeleteUser("UserName");

To get the user object:


MembershipUser user = provider.GetUser("UserName", false);

To update user data after change:


provider.UpdateUser(UserObject);