Strony internetowe ASP.NET — obiekt WebSecurity
Opis
Obiekt WebSecurity zapewnia zabezpieczenia i uwierzytelnianie aplikacji ASP.NET Web Pages.
Za pomocą obiektu WebSecurity możesz tworzyć konta użytkowników, logować się i wylogowywać użytkowników, resetować lub zmieniać hasła i nie tylko.
Dokumentacja obiektu WebSecurity — Właściwości
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 |
Dokumentacja obiektu WebSecurity — metody
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 |
Inicjowanie bazy danych WebSecurity
Musisz utworzyć lub zainicjować bazę danych WebSecurity przed użyciem obiektu WebSecurity w kodzie.
W katalogu głównym sieci Web utwórz stronę (lub edytuj stronę) o nazwie _AppStart.cshtml .
Umieść następujący kod w pliku:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
Powyższy kod będzie uruchamiany przy każdym uruchomieniu strony internetowej (aplikacji). Inicjuje bazę danych WebSecurity.
„Użytkownicy” to nazwa bazy danych WebSecurity (Users.sdf).
„UserProfile” to nazwa tabeli bazy danych zawierającej informacje o profilu użytkownika.
„UserId” to nazwa kolumny zawierającej identyfikatory użytkowników (klucz podstawowy).
„E-mail” to nazwa kolumny zawierającej nazwy użytkowników.
Ostatni parametr true to wartość logiczna wskazująca, że profil użytkownika i tabele członkostwa powinny być tworzone automatycznie, jeśli nie istnieją, w przeciwnym razie false .
Chociaż prawda oznacza automatyczne tworzenie tabel bazy danych, sama baza danych nie zostanie utworzona automatycznie. Musi istnieć.
Baza danych bezpieczeństwa sieci
Tabela UserProfile zawiera jeden rekord dla każdego użytkownika, z identyfikatorem użytkownika (klucz podstawowy) i nazwą użytkownika (e-mail):
UserId | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
Tabela Członkostwo będzie zawierała informacje o członkostwie o tym, kiedy użytkownik został utworzony i czy (i kiedy) członkostwo zostało potwierdzone.
Podobnie jak to (niektóre kolumny nie są pokazane):
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 |
Prosta konfiguracja członkostwa
Mogą wystąpić błędy przy użyciu obiektu WebSecurity, jeśli witryna nie jest skonfigurowana do korzystania z systemu członkostwa ASP.NET Web Pages SimpleMembership .
Może się tak zdarzyć, jeśli serwer dostawcy hostingu jest skonfigurowany inaczej niż serwer lokalny. Aby rozwiązać ten problem, dodaj następujący element do pliku Web.config witryny:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>