ASP.NET Web Sayfaları - WebSecurity Nesnesi
Açıklama
WebSecurity Nesnesi , ASP.NET Web Sayfaları uygulamaları için güvenlik ve kimlik doğrulama sağlar.
WebSecurity nesnesi ile kullanıcı hesapları oluşturabilir, kullanıcılarda oturum açabilir ve oturumu kapatabilir, parolaları sıfırlayabilir veya değiştirebilir ve daha fazlasını yapabilirsiniz.
WebSecurity Nesne Başvurusu - Özellikler
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 Nesne Başvurusu - Yöntemler
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 |
WebSecurity Veritabanını Başlatma
Kodunuzda WebSecurity nesnesini kullanmadan önce bir WebSecurity veritabanı oluşturmanız veya başlatmanız gerekir.
Web'inizin kökünde, _AppStart.cshtml adlı bir sayfa oluşturun (veya sayfayı düzenleyin) .
Dosyanın içine aşağıdaki kodu koyun:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
Yukarıdaki kod, web sitesi (uygulama) her başlatıldığında çalışacaktır. WebSecurity veritabanını başlatır.
"Kullanıcılar" , WebSecurity veritabanının (Users.sdf) adıdır.
"Kullanıcı Profili" , kullanıcı profili bilgilerini içeren veritabanı tablosunun adıdır.
"UserId" , kullanıcı kimliklerini (birincil anahtar) içeren sütunun adıdır.
"E-posta" , kullanıcı adlarını içeren sütunun adıdır.
Son parametre true , kullanıcı profili ve üyelik tablolarının yoksa otomatik olarak oluşturulması gerektiğini belirten bir boole değeridir, aksi takdirde false .
true , veritabanı tablolarının otomatik olarak oluşturulmasını gösterse de, veritabanının kendisi otomatik olarak oluşturulmaz. O var olmalı.
Web Güvenliği Veritabanı
Kullanıcı Profili tablosu, her kullanıcı için bir kullanıcı kimliği (birincil anahtar) ve kullanıcı adı (e-posta) ile bir kayıt içerir:
UserId | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
Üyelik tablosu , kullanıcının ne zaman oluşturulduğu ve üyeliğin onaylanıp onaylanmadığı (ve ne zaman) hakkında üyelik bilgilerini içerecektir.
Bunun gibi (bazı sütunlar gösterilmemiştir):
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 |
Basit Üyelik Yapılandırması
Siteniz ASP.NET Web Sayfaları üyelik sistemi SimpleMembership'i kullanacak şekilde yapılandırılmamışsa, WebSecurity nesnesini kullanırken hatalar alabilirsiniz .
Bu, bir barındırma sağlayıcısının sunucusu yerel sunucunuzdan farklı şekilde yapılandırılmışsa oluşabilir. Bunu düzeltmek için sitenin Web.config dosyasına aşağıdaki öğeyi ekleyin:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>