CreateUserAndAccount (WebSecurity Ref)

The CreateUserAndAccount method creates a user account. It is slightly different from CreateAccount, as it accepts 4 arguments: username, password, additional user info (such as DOB) and verification token. The last two are optional while the first two are mandatory.

Syntax

WebSecurity.CreateUserAndAccount(string username, string password, object properties, bool confirmaccount)

In the following example we create a user account with additional user information, which is FirstName, LastName and Country. In your Users table make sure you have the following columns if you wish to test this out.

Sponsored Links
Name Data Type Default Value Is Primary Key? Is Identity? Allow Nulls
Email Nvarchar(4000) Null No No Yes
UserID Int Null Yes Yes No
Forename Nvarchar(50) Null No No Yes
Surname Nvarchar(50) Null No No Yes
Country Nvarchar(200) Null No No Yes

Example

C# Code

//set general variables
string username = "";
string newpassword = "";
string confirmpassword = "";
string Forename = "";
string Surname = "";
string Country = "";
string msg ="";
 
 
//proccess data on post
if (IsPost)
{
    
    username = Request["username"];
    newpassword = Request["newpassword"];
    confirmpassword = Request["confirmpassword"];
    Forename = Request["Forename"];
    Surname = Request["Surname"];
    Country = Request["Country"];
 
 
    //MAKE SURE FORMS ARE NOT NULL
    if (username.IsEmpty())
    {
        ModelState.AddError("username", "Please enter a username");
    }
 
    if (newpassword.IsEmpty())
    {
       ModelState.AddError("newpassword", "Please enter a new password");
    }
    
    //MAKE SURE NEW PASSWORD AND CONFIRM PASSWORDS MATCH
    if (newpassword != confirmpassword)
    {
        ModelState.AddError("confirmpassword", "Passwords do not match");
    }
 
     if (Forename.IsEmpty())
    {
       ModelState.AddError("Forename", "Please enter Forename");
    }
 
     if (Surname.IsEmpty())
    {
       ModelState.AddError("Surname", "Please enter Surname");
    }
 
     if (Country.IsEmpty())
    {
       ModelState.AddError("Country", "Please enter your Country");
    }
 
 
    if (ModelState.IsValid)
    {
               
        try
        {
          
        var userInfo = new{Forename = Forename, Surname = Surname, Country = Country};
        WebSecurity.CreateUserAndAccount(username, newpassword, userInfo, false);
        msg = "Account Created";          
 
        }catch (Exception ex)
        {
           msg = ex.Message;
        }
    }
}
 
 
}

HTML

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        @msg
        @Html.ValidationSummary(true)
        <form method="post">
            <div>
        <label>Username</label>
            <input type="text" value="@username" name="username"/>
            @Html.ValidationMessage("username")
                </div>
              <div>
             <label>New Password</label>
              <input type="password" name="newpassword"/>
               @Html.ValidationMessage("newpassword")
            </div>
 
              <div>
             <label>Confirm Password</label>
              <input type="password" name="confirmpassword"/>
               @Html.ValidationMessage("confirmpassword")
            </div>
 
               <div>
             <label>Forename</label>
              <input type="text" value="@Forename" name="Forename"/>
               @Html.ValidationMessage("forename")
            </div>
 
               <div>
             <label>Surname</label>
              <input type="text" value="@Surname" name="Surname"/>
               @Html.ValidationMessage("Surname")
            </div>
 
               <div>
             <label>Country</label>
              <input type="text" value="@Country" name="Country"/>
               @Html.ValidationMessage("Country")
            </div>
 
                   <input type="submit"/>
        </form>
 
    </body> 
</html>

See Also

ConfirmAccount