Home > ASP.Net > Create a new User via code

Create a new User via code


try this example

<asp:Label runat="server" id="InvalidUserNameOrPasswordMessage" Visible="false" EnableViewState="false" ForeColor="Red"></asp:Label>
        Enter a username:
        <asp:TextBox ID="Username" runat="server"></asp:TextBox>
        <br />
        Choose a password:
        <asp:TextBox ID="Password" TextMode="Password" runat="server"></asp:TextBox>       
        <br />
        Enter your email address:
        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
        <br />
        <asp:Label runat="server" ID="SecurityQuestion"></asp:Label>:
        <asp:TextBox ID="SecurityAnswer" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="CreateAccountButton" runat="server"
            Text="Create the User Account" onclick="CreateAccountButton_Click" />
        <asp:Label ID="CreateAccountResults" runat="server"></asp:Label>

and in Code Behind:

protected void CreateAccountButton_Click(object sender, EventArgs e)
     MembershipCreateStatus createStatus;
     MembershipUser newUser = Membership.CreateUser(Username.Text, Password.Text, Email.Text, passwordQuestion, SecurityAnswer.Text, true, out createStatus);
     switch (createStatus)
          case MembershipCreateStatus.Success:
               CreateAccountResults.Text = "The user account was successfully created!";
          case MembershipCreateStatus.DuplicateUserName:
               CreateAccountResults.Text = "There already exists a user with this username.";
          case MembershipCreateStatus.DuplicateEmail:
               CreateAccountResults.Text = "There already exists a user with this email address.";
          case MembershipCreateStatus.InvalidEmail:
               CreateAccountResults.Text = "There email address you provided in invalid.";
          case MembershipCreateStatus.InvalidAnswer:
               CreateAccountResults.Text = "There security answer was invalid.";
          case MembershipCreateStatus.InvalidPassword:
               CreateAccountResults.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
               CreateAccountResults.Text = "There was an unknown error; the user account was NOT created.";

and in web.config

<authentication mode="Forms">
   <forms loginUrl="Login.aspx" timeout="300000" protection="All" path="/" slidingExpiration="false"></forms>
  <membership defaultProvider="MyProvider" userIsOnlineTimeWindow="25">
    <add name="MyProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="Bag_ConnStr" applicationName="/" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
  <roleManager defaultProvider="TaskRoleProvider" enabled="true">
    <add name="TaskRoleProvider" connectionStringName="Bag_ConnStr" applicationName="/" type="System.Web.Security.SqlRoleProvider"/>
  <!--SiteMap Provider-->
  <siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
    <add name="XmlSiteMapProvider" description="Default SiteMap provider." type="System.Web.XmlSiteMapProvider" siteMapFile="web.sitemap" securityTrimmingEnabled="true"/>

just change connection string name with your’s

Good Luck

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: