ASP.NET Web Pages - WebSecurity 對象

描述

WebSecurity 對象提供為 ASP.NET Web Pages 應用程序提供安全和認證。

通過 WebSecurity 對象,您能夠創建用戶賬戶、登錄和登出,重置或更改密碼等等。

WebSecurity 對象參考手冊 - 屬性

屬性 描述
CurrentUserId 獲取當前用戶的 ID。
CurrentUserName 獲取當前用戶的名稱。
HasUserId 獲取指示當前用戶是否具有用戶 ID 的值。如果是,返回 true。
IsAuthenticated 獲取當前用戶的身份驗證狀態。

WebSecurity 對象參考手冊 - 方法

屬性 描述
ChangePassword() 更改用戶的密碼。
ConfirmAccount() 確認賬戶有效並激活該賬戶。
CreateAccount() 創建新的用戶賬戶。
CreateUserAndAccount() 創建新的用戶賬戶。
GeneratePasswordResetToken() 生成可以通過電子郵件發送給用戶的密碼重置令牌。
GetCreateDate() 返回創建指定會員資格賬戶的日期和時間。
GetPasswordChangeDate() 返回最近更改指定會員資格賬戶密碼的日期和時間。
GetPasswordFailures
SinceLastSuccess()
返回自上次成功登錄或創建會員資格賬戶以來錯誤輸入密碼的次數。
GetUserId() 基於指定用戶名返回用戶 ID。
GetUserIdFrom
PasswordResetToken ()
Return the user ID from the password reset token.
InitializeDatabaseConnection() Initialize the membership system by connecting to a database that contains user information.
IsAccountLockedOut() Indicate whether the specified membership account has been locked out due to too many failed password attempts.
IsConfirmed() Return a value indicating whether the user has already been confirmed.
IsCurrentUser() Return a value indicating whether the username of the logged-in user matches the specified username.
Login() Login the user.
Logout() Logout the user.
RequireAuthenticatedUser() If the user is not authenticated, set the HTTP status to 401 (Unauthorized).
RequireRoles() If the current user does not belong to the specified role, set the HTTP status code to 401.
RequireUser() If the current user is not the specified user, set the HTTP status to 401.
ResetPassword() Reset the password using the password reset token.
UserExists() Check kung may umiiral na user.

Technical Data

Name Value
Class WebMatrix.WebData.WebSecurity
Namespace WebMatrix.WebData
Assembly WebMatrix.WebData.dll

Inilalapat ang WebSecurity Database

Bago gamitin ang WebSecurity object sa code, dapat lumikha o inilalapat ang WebSecurity database.

Sa web directory, lumikha o i-edit ang pahina na _AppStart.cshtml.

Maglagay ng sumusunod na code sa file na ito:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", 
true);
}

Ang website ay magpapatok para sa mga ito kada pagpapatok ng website. Ito ay magpapatayuan ng database na WebSecurity.

"Users" ay isang pangalan ng database ng WebSecurity (Users.sdf).

"UserProfile" ay isang pangalan ng database table na naglalaman ng mga impormasyon ng configuration ng user.

"UserId" คือชื่อคอลัมน์ที่มี ID ผู้ใช้ (หลัก)

"Email" คือชื่อคอลัมน์ที่มีชื่อผู้ใช้

ตัวเลขที่สุดแห่งค่า true คือค่าเลือกทางวิชาการที่ชี้ว่าควรทำการสร้างตารางข้อมูลบัญชีผู้ใช้และตารางสมาชิก (ถ้ามันไม่มีอยู่) หรือไม่

คำเตือน: ถึงแม้ว่า true จะชี้ว่าจะทำการสร้างตารางฐานข้อมูลโดยอัตโนมัติ แต่ฐานข้อมูลเองไม่ได้ถูกสร้างโดยอัตโนมัติ มันต้องมีอยู่

ฐานข้อมูล WebSecurity

บันทึกทุกของ UserProfile มีการแสดงถึงผู้ใช้ที่มีในระบบ ซึ่งรวมถึง UserId (หลัก) และชื่อผู้ใช้ (อีเมล):

UserId อีเมล
1 john@johnson.net
2 peter@peterson.com
3 lars@larson.eut

ตาราง Membership มีข้อมูลสมาชิกที่เกี่ยวข้อง เช่น ว่าใครที่สร้างบัญชีและใครที่ยืนยันสมาชิก (และเมื่อใด)

คล้ายนี้ (บางคอลัมน์ไม่ได้ระบุ):

UserId วันที่สร้าง ยืนยัน
ตะกร้าสาร
คุณ
ยืนยัน
สุดท้าย
รหัสผ่านผิด
รหัสผ่าน เปลี่ยนรหัสผ่าน
1 12.04.2012 16:12:17 NULL True NULL AFNQhWfy.... 12.04.2012 16:12:17

หมายเหตุ:ถ้าคุณต้องการเห็นคอลัมน์ทั้งหมดและเนื้อหาทั้งหมด โปรดเปิดฐานข้อมูลด้วย WebMatrix และตรวจสอบแต่ละตาราง

การตั้งค่าสมาชิกง่ายๆ

ถ้าเว็บไซต์ของคุณยังไม่ถูกตั้งค่าเพื่อใช้ระบบสมาชิกของ ASP.NET Web Pages SimpleMembership อาจจะเกิดข้อผิดพลาดขณะใช้ WebSecurity ออบเจ็กเต็ก

ถ้าการตั้งค่าของผู้ให้บริการเซิร์ฟเวอร์ต่างจากเซิร์ฟเวอร์ท้องถิ่นของคุณ จะเกิดข้อผิดพลาดขึ้น ในการแก้ปัญหานี้ โปรดเพิ่มองค์ประกอบต่อไปนี้ในแบบฟอร์ม Web.config ของเว็บไซต์ของคุณ:

<appSettings> 
<add key="enableSimpleMembership" value="true" /> 
</appSettings>