System.Web.Security.Membership.CreateUser Method

Adds a new user to the data store.


public static MembershipUser CreateUser (string username, string password)


The user name for the new user.
The password for the new user.


A System.Web.Security.MembershipUser object for the newly created user.


Membership.CreateUser(string, string) adds a new user to the data store and returns a System.Web.Security.MembershipUser object for the newly created user. If the user creation fails, a System.Web.Security.MembershipCreateUserException is thrown. You can retrieve a System.Web.Security.MembershipCreateStatus value from the MembershipCreateUserException.StatusCode property of the System.Web.Security.MembershipCreateUserException that indicates why user creation failed.

Once a membership user has been created and you have a reference to a System.Web.Security.MembershipUser object for that user, you can modify the settings for that user with the System.Web.Security.MembershipUser public methods, such as MembershipUser.ChangePasswordQuestionAndAnswer(string, string, string) for applications where Membership.RequiresQuestionAndAnswer is true, or by setting the property values of the System.Web.Security.MembershipUser object and passing them to the Membership.UpdateUser(MembershipUser) method.

If a user already exists in the data source for the application, you can obtain a System.Web.Security.MembershipUser object for the existing user with the Membership.GetUser method.

The System.Web.Security.SqlMembershipProvider provides an option to require a unique e-mail address for each user. If the SqlMembershipProvider.RequiresUniqueEmail property is true, you will need to use one of the Membership.CreateUser(string, string) overloads that allows you to specify an e-mail address for the user being created. Otherwise, a System.Web.Security.MembershipCreateUserException will be thrown.

Leading and trailing spaces are trimmed from all parameter values.


Namespace: System.Web.Security
Assembly: System.Web (in System.Web.dll)
Assembly Versions:
Since: .NET 2.0