ASP.NET Web Pages - WebSecurity Object
Description
WebSecurity objectProvides security and authentication for ASP.NET Web Pages applications.
Through the WebSecurity object, you can create user accounts, log users in and out, reset or change passwords, and perform many other security-related functions.
WebSecurity Object Reference - Properties
| property | Description |
|---|---|
| CurrentUserId | Gets the ID of the currently logged-in user. |
| CurrentUserName | Gets the name of the currently logged-in user. |
| HasUserId | Returns true if there is a current user ID. |
| IsAuthenticated | Returns true if the current user is logged in. |
WebSecurity Object Reference - Methods
| Method | Description |
|---|---|
| ChangePassword() | Changes the password for the specified user. |
| ConfirmAccount() | Confirms an account using an account confirmation token. |
| CreateAccount() | Creates a new user account. |
| CreateUserAndAccount() | Creates a new user account. |
| GeneratePasswordResetToken() | Generates a password reset token that can be sent to the user via email so the user can reset the password. |
| GetCreateDate() | Gets the time the specified membership was created. |
| GetPasswordChangeDate() | Gets the date and time of the password change. |
| GetUserId() | Gets the user ID based on the user name. |
| InitializeDatabaseConnection() | Initializes the WebSecurity system (database). |
| IsConfirmed() | Checks whether the user has been confirmed. Returns true if confirmed (for example, via email). |
| IsCurrentUser() | Checks whether the current user's name matches the specified user name. Returns true if it matches. |
| Login() | Sets the authentication token and logs the user in. |
| Logout() | Removes the authentication token and logs the user out. |
| RequireAuthenticatedUser() | If the user is not authenticated, sets the HTTP status to 401 (Unauthorized). |
| RequireRoles() | If the current user is not a member of the specified role, sets the HTTP status to 401 (Unauthorized). |
| RequireUser() | If the current user is not the user with the specified user name, sets the HTTP status to 401 (Unauthorized). |
| ResetPassword() | If the password reset token is valid, changes the user's password to the new password. |
| UserExists() | Checks whether the specified user exists. |
Technical data
| Name | Value |
|---|---|
| Class | WebMatrix.WebData.WebSecurity |
| Namespace | WebMatrix.WebData |
| Assembly | WebMatrix.WebData.dll |
Initializing the WebSecurity Database
If you want to use the WebSecurity object in your code, you must first create or initialize the WebSecurity database.
In your web root, create a page named _AppStart.cshtml (if it already exists, simply edit the page).
Copy the following code into the file:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}
The code above will run every time the website (application) starts. It initializes the WebSecurity database.
"Users" is the name of the WebSecurity database (Users.sdf).
"UserProfile" is the name of the database table containing user profile information.
"UserId" is the name of the column containing the user ID (primary key).
"Email" is the name of the column containing the user name.
The last parameter true is a Boolean value indicating that if the user profile table and membership table do not exist, the tables will be created automatically. If you do not want to create tables automatically, set the parameter to false。
![]() |
Although true to indicate automatic database creation table, but the database will not be created automatically. Therefore the database must exist. |
|---|
WebSecurity Database
UserProfile The table creates and saves one record per user, with the user ID (primary key) and user name (email):
| UserId | |
|---|---|
| 1 | john@johnson.net |
| 2 | peter@peterson.com |
| 3 | lars@larson.eut |
Membership The table contains membership information, such as when the user was created, whether the member is confirmed, when the member was confirmed, and so on.
As shown below (some columns are not displayed):
| User
Id | Create
Date | Confirmation
Token | Is
Confirmed | Last
Password
Failure | Password | Password
Change | | --- | --- | --- | --- | --- | --- | --- | | 1 | 12.04.2012 16:12:17 | NULL | True | NULL | AFNQhWfy.... | 12.04.2012 16:12:17 |
Note: If you want to see all columns and content, open the database and look at each table inside it.
Simple Membership Configuration
When you use the WebSecurity object, if your site is not configured to use the ASP.NET Web Pages membership system SimpleMembership, an error may occur.
If the configuration of your hosting provider's server differs from your local server's configuration, an error may also occur. To resolve this, add the following element to the Web.config file of your website:
