ASP.NET webpagina's - WebSecurity Object


Beschrijving

Het WebSecurity Object biedt beveiliging en authenticatie voor ASP.NET Web Pages-toepassingen.

Met het WebSecurity-object kunt u gebruikersaccounts maken, gebruikers in- en uitloggen, wachtwoorden opnieuw instellen of wijzigen, en meer.


WebSecurity Object Reference - Eigenschappen

Properties Description
CurrentUserId Gets the ID for the current user
CurrentUserName Gets the name of the current user
HasUserId Returns true if the current has a user ID
IsAuthenticated Returns true if the current user is logged in

WebSecurity Object Reference - Methoden

Method Description
ChangePassword() Changes the password for a user
ConfirmAccount() Confirms an account using a confirmation token
CreateAccount() Creates a new user account
CreateUserAndAccount() Creates a new user account
GeneratePasswordResetToken() Generates a token that can be sent to as user by email
GetCreateDate() Gets the time the specified membership was created
GetPasswordChangeDate() Gets the date and time when password was changed
GetUserId() Gets a user ID from a user name
InitializeDatabaseConnection() Initializes the WebSecurity system (database)
IsConfirmed() Checks if a user is confirmed
IsCurrentUser() Checks if the current user matches a user name
Login() Logs the user in by setting a token in the cookie
Logout() Logs the user out by removing the token cookie
RequireAuthenticatedUser() Exits the page if the user is not an authenticated user
RequireRoles() Exits the page if the user is not a part of the specified roles
RequireUser() Exits the page if the user is not the specified user
ResetPassword() Changes a user's password using a token
UserExists() Checks if a given user exists


De webbeveiligingsdatabase initialiseren

U moet een WebSecurity-database maken of initialiseren voordat u het WebSecurity-object in uw code kunt gebruiken.

Maak in de hoofdmap van uw web een pagina (of bewerk de pagina ) met de naam _AppStart.cshtml .

Zet de volgende code in het bestand:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}

De bovenstaande code wordt uitgevoerd elke keer dat de website (applicatie) wordt gestart. Het initialiseert de WebSecurity-database.

"Gebruikers" is de naam van de WebSecurity-database (Users.sdf).

"UserProfile" is de naam van de databasetabel die de gebruikersprofielinformatie bevat.

"UserId" is de naam van de kolom die de gebruikers-ID's (primaire sleutel) bevat.

"E-mail" is de naam van de kolom die gebruikersnamen bevat.

De laatste parameter true is een booleaanse waarde die aangeeft dat het gebruikersprofiel en lidmaatschapstabellen automatisch moeten worden gemaakt als ze niet bestaan, anders false .

Hoewel true aangeeft dat de databasetabellen automatisch worden gemaakt , wordt de database zelf niet automatisch gemaakt. Het moet bestaan.


De webbeveiligingsdatabase

De tabel UserProfile bevat één record voor elke gebruiker, met een gebruikers-ID (primaire sleutel) en de naam van de gebruiker (e-mail):

UserId Email
1 [email protected]
[email protected]
3 [email protected]

De lidmaatschapstabel bevat lidmaatschapsinformatie over wanneer de gebruiker is gemaakt en of (en wanneer) het lidmaatschap is bevestigd.

Ongeveer zoals dit (sommige kolommen worden niet getoond):

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

Eenvoudige lidmaatschapsconfiguratie

U kunt fouten krijgen bij het gebruik van het WebSecurity-object als uw site niet is geconfigureerd voor het gebruik van het ASP.NET Web Pages-lidmaatschapssysteem SimpleMembership .

Dit kan gebeuren als de server van een hostingprovider anders is geconfigureerd dan uw lokale server. Om dit op te lossen, voegt u het volgende element toe aan het Web.config-bestand van de site:

<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>