Met Websecurity Createaccount
## WebSecurity.CreateAccount() Method
The `WebSecurity.CreateAccount()` method is a core function of the ASP.NET Web Pages `WebSecurity` helper. It creates a new user account in the membership database using a specified username and password. It also provides an optional parameter to require account confirmation before the user can log in.
---
## Definition
This method registers a new user account in the database. If account confirmation is required, the method generates and returns a unique confirmation token that can be sent to the user (typically via email) to verify their identity.
---
## Syntax
This method is available in both C# and VB.NET.
```csharp
WebSecurity.CreateAccount(username, password, requireConfirmation)
```
---
## Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `userName` | `String` | The unique username for the new account. |
| `password` | `String` | The password for the new account. |
| `requireConfirmation` | `Boolean` | *(Optional)* Set to `true` if the account must be confirmed using a token before the user can log in. The default value is `false`. |
---
## Return Value
| Type | Description |
| :--- | :--- |
| `String` | A unique confirmation token. This token is returned only if `requireConfirmation` is set to `true`. You can send this token to the user's email address to complete the registration process. |
---
## Code Example
Below is a practical C# example demonstrating how to use `WebSecurity.CreateAccount()` in an ASP.NET Web Pages (Razor) application.
```cshtml
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Register";
var email = "";
var password = "";
var confirmationToken = "";
if (IsPost) {
email = Request;
password = Request;
if (!email.IsEmpty() && !password.IsEmpty()) {
try {
// Create the account with confirmation required
confirmationToken = WebSecurity.CreateAccount(email, password, requireConfirmation: true);
// Code to send the confirmationToken to the user's email goes here
// e.g., WebMail.Send(email, "Confirm your account", "Your token is: " + confirmationToken);
Response.Redirect("~/Account/RegisterSuccess");
}
catch (Exception ex) {
ModelState.AddFormError(ex.Message);
}
}
}
}
```
---
## Errors and Exceptions
### InvalidOperationException
An `InvalidOperationException` is thrown if you attempt to call `CreateAccount()` under the following conditions:
* The `InitializeDatabaseConnection()` method has not been called to initialize the membership system.
* **SimpleMembership** is not initialized or has been explicitly disabled in the website configuration.
### MembershipCreateUserException
A `MembershipCreateUserException` is thrown if the account creation fails due to any of the following reasons:
* The username is null or empty.
* The username already exists in the membership database.
* The password is null or empty.
* The password does not meet length or complexity requirements.
* A database operation error occurs during the creation process.
---
## Technical Specifications
| Property | Value |
| :--- | :--- |
| **Namespace** | `WebMatrix.WebData` |
| **Assembly** | `WebMatrix.WebData.dll` |
YouTip