Pagine Web ASP.NET - Oggetto WebSecurity
Descrizione
L' oggetto WebSecurity fornisce sicurezza e autenticazione per le applicazioni delle pagine Web ASP.NET.
Con l'oggetto WebSecurity è possibile creare account utente, accedere e disconnettersi utenti, reimpostare o modificare password e altro ancora.
Riferimento oggetto WebSecurity - Proprietà
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 |
Riferimento all'oggetto WebSecurity - Metodi
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 |
Inizializzazione del database WebSecurity
È necessario creare o inizializzare un database WebSecurity prima di poter utilizzare l'oggetto WebSecurity nel codice.
Nella radice del tuo Web, crea una pagina (o modifica la pagina) denominata _AppStart.cshtml .
Metti il seguente codice all'interno del file:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
Il codice sopra verrà eseguito ogni volta che il sito Web (applicazione) viene avviato. Inizializza il database WebSecurity.
"Utenti" è il nome del database WebSecurity (Utenti.sdf).
"UserProfile" è il nome della tabella del database che contiene le informazioni sul profilo utente.
"UserId" è il nome della colonna che contiene gli ID utente (chiave primaria).
"Email" è il nome della colonna che contiene i nomi utente.
L'ultimo parametro true è un valore booleano che indica che il profilo utente e le tabelle di appartenenza devono essere create automaticamente se non esistono, altrimenti false .
Sebbene true indichi la creazione automatica delle tabelle del database, il database stesso non verrà creato automaticamente. Deve esistere.
Il database di WebSecurity
La tabella UserProfile contiene un record per ogni utente, con un ID utente (chiave primaria) e il nome dell'utente (e-mail):
UserId | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
La tabella Appartenenza conterrà informazioni sull'appartenenza su quando è stato creato l'utente e se (e quando) l'appartenenza è stata confermata.
Proprio così (alcune colonne non sono mostrate):
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 |
Configurazione semplice dell'abbonamento
È possibile che vengano visualizzati errori utilizzando l'oggetto WebSecurity, se il sito non è configurato per utilizzare il sistema di appartenenza alle pagine Web ASP.NET SimpleMembership .
Ciò può verificarsi se il server di un provider di hosting è configurato in modo diverso dal server locale. Per risolvere il problema, aggiungi il seguente elemento al file Web.config del sito:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>