Poradniki ASP

ASP DOM

Samouczek WP

Wprowadzenie do stron internetowych Strony internetowe Brzytwa Układ stron internetowych Foldery stron internetowych Globalne strony internetowe Formularze stron internetowych Obiekty stron internetowych Pliki stron internetowych Bazy danych stron internetowych Pomocnicy stron internetowych Strony internetowe WebGrid Wykresy stron internetowych Adres e-mail stron internetowych Bezpieczeństwo stron internetowych Strony internetowe Opublikuj Przykłady stron internetowych Klasy stron internetowych

Brzytwa ASP.NET

Wprowadzenie do brzytwy Składnia brzytwy Zmienne C# brzytwy Pętle C# brzytwy Razor C# Logika Zmienne VB brzytwy Pętle Razor VB Razor VB Logic

ASP klasyczny

Wprowadzenie do ASP Składnia ASP Zmienne ASP Procedury ASP Warunki korzystania z ASP Zapętlanie ASP Formularze ASP Pliki cookie ASP Sesja ASP Aplikacja ASP ASP #zawiera ASP Global.as ASP AJAX e-mail ASP Przykłady ASP

Odniesienie do ASP

Funkcje ASP VB Słowa kluczowe ASP VB Odpowiedź ASP Zapytanie ASP Aplikacja ASP Sesja ASP Serwer ASP Błąd ASP System plików ASP Strumień tekstowy ASP Dysk ASP Plik ASP Folder ASP Słownik ASP ASP AdRotator Przeglądarka ASP Łączenie treści ASP Rotator treści ASP Szybkie ref. ASP

Samouczek ADO

Wprowadzenie ADO Connect Zestaw rekordów ADO Wyświetlacz ADO Zapytanie ADO Sortuj ADO ADO Dodaj Aktualizacja ADO ADO Usuń Demo ADO Przyspieszenie ADO

Obiekty ADO

ADO Polecenie Połączenie ADO Błąd ADO Pole ADO Parametr ADO Nieruchomość ADO Rekord ADO Zestaw rekordów ADO Strumień ADO Typy danych ADO

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 Email
1 [email protected]
[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>