Halaman Web ASP.NET - Objek Keamanan Web
Keterangan
Objek WebSecurity menyediakan keamanan dan otentikasi untuk aplikasi Halaman Web ASP.NET.
Dengan objek WebSecurity Anda dapat membuat akun pengguna, pengguna masuk dan keluar, mengatur ulang atau mengubah kata sandi, dan banyak lagi.
Referensi Objek Keamanan Web - Properti
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 |
Referensi Objek Keamanan Web - Metode
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 |
Menginisialisasi Basis Data Keamanan Web
Anda harus membuat atau menginisialisasi database WebSecurity sebelum Anda dapat menggunakan objek WebSecurity dalam kode Anda.
Di root web Anda, buat halaman (atau edit halaman ) bernama _AppStart.cshtml .
Masukkan kode berikut di dalam file:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
Kode di atas akan berjalan setiap kali situs web (aplikasi) dimulai. Ini menginisialisasi database WebSecurity.
"Pengguna" adalah nama database WebSecurity (Users.sdf).
"Profil Pengguna" adalah nama tabel database yang berisi informasi profil pengguna.
"UserId" adalah nama kolom yang berisi ID pengguna (kunci utama).
"Email" adalah nama kolom yang berisi nama pengguna.
Parameter terakhir true adalah nilai boolean yang menunjukkan bahwa profil pengguna dan tabel keanggotaan harus dibuat secara otomatis jika tidak ada, jika tidak false .
Meskipun true menunjukkan pembuatan tabel database secara otomatis, database itu sendiri tidak akan dibuat secara otomatis. Itu harus ada.
Basis Data Keamanan Web
Tabel UserProfile berisi satu record untuk setiap pengguna, dengan ID pengguna (kunci utama) dan nama pengguna (email):
UserId | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
Tabel Keanggotaan akan berisi informasi keanggotaan tentang kapan pengguna dibuat dan jika (dan kapan) keanggotaan dikonfirmasi.
Mirip seperti ini (beberapa kolom tidak ditampilkan):
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 |
Konfigurasi Keanggotaan Sederhana
Anda mungkin mendapatkan kesalahan menggunakan objek WebSecurity, jika situs Anda tidak dikonfigurasi untuk menggunakan sistem keanggotaan ASP.NET Web Pages SimpleMembership .
Ini dapat terjadi jika server penyedia hosting dikonfigurasi secara berbeda dari server lokal Anda. Untuk memperbaikinya, tambahkan elemen berikut ke file Web.config situs:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>