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

Create a new User via code


Hi

try this example

<asp:Label runat="server" id="InvalidUserNameOrPasswordMessage" Visible="false" EnableViewState="false" ForeColor="Red"></asp:Label>
    </p>
   
    <p>
        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" />
    </p>
    <p>
        <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!";
               break;
          case MembershipCreateStatus.DuplicateUserName:
               CreateAccountResults.Text = "There already exists a user with this username.";
               break;
          case MembershipCreateStatus.DuplicateEmail:
               CreateAccountResults.Text = "There already exists a user with this email address.";
               break;
          case MembershipCreateStatus.InvalidEmail:
               CreateAccountResults.Text = "There email address you provided in invalid.";
               break;
          case MembershipCreateStatus.InvalidAnswer:
               CreateAccountResults.Text = "There security answer was invalid.";
               break;
          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.";
               break;
          default:
               CreateAccountResults.Text = "There was an unknown error; the user account was NOT created.";
               break;
     }
}

and in web.config

<authentication mode="Forms">
   <forms loginUrl="Login.aspx" timeout="300000" protection="All" path="/" slidingExpiration="false"></forms>
  </authentication>
  <membership defaultProvider="MyProvider" userIsOnlineTimeWindow="25">
   <providers>
    <add name="MyProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, 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=""/>
   </providers>
  </membership>
  <roleManager defaultProvider="TaskRoleProvider" enabled="true">
   <providers>
    <add name="TaskRoleProvider" connectionStringName="Bag_ConnStr" applicationName="/" type="System.Web.Security.SqlRoleProvider"/>
   </providers>
  </roleManager>
  <!--SiteMap Provider-->
  <siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
   <providers>
    <add name="XmlSiteMapProvider" description="Default SiteMap provider." type="System.Web.XmlSiteMapProvider" siteMapFile="web.sitemap" securityTrimmingEnabled="true"/>
   </providers>
  </siteMap>

just change connection string name with your’s

Good Luck

Advertisements
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: