หน้าเว็บ ASP.NET - WebSecurity Object
คำอธิบาย
WebSecurity Objectจัดเตรียมความปลอดภัยและการรับรองความถูกต้องสำหรับแอปพลิเคชัน ASP.NET Web Pages
ด้วยอ็อบเจ็กต์ WebSecurity คุณสามารถสร้างบัญชีผู้ใช้ ผู้ใช้ที่เข้าสู่ระบบและออกจากระบบ รีเซ็ตหรือเปลี่ยนรหัสผ่าน และอื่นๆ
การอ้างอิงออบเจ็กต์ความปลอดภัยบนเว็บ - คุณสมบัติ
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 |
การอ้างอิงออบเจ็กต์ความปลอดภัยบนเว็บ - เมธอด
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
คุณต้องสร้างหรือเริ่มต้นฐานข้อมูล WebSecurity ก่อนที่คุณจะสามารถใช้วัตถุ WebSecurity ในโค้ดของคุณได้
ในรูทของเว็บ ให้สร้างเพจ (หรือแก้ไขเพจ ) ชื่อ_AppStart.cshtml
ใส่รหัสต่อไปนี้ในไฟล์:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
รหัสด้านบนจะทำงานทุกครั้งที่เว็บไซต์ (แอปพลิเคชัน) เริ่มทำงาน มันเริ่มต้นฐานข้อมูล WebSecurity
"ผู้ใช้"คือชื่อของฐานข้อมูล WebSecurity (Users.sdf)
"UserProfile"คือชื่อของตารางฐานข้อมูลที่มีข้อมูลโปรไฟล์ผู้ใช้
"UserId"คือชื่อของคอลัมน์ที่มี ID ผู้ใช้ (คีย์หลัก)
"อีเมล"คือชื่อของคอลัมน์ที่มีชื่อผู้ใช้
พารามิเตอร์สุดท้ายtrueคือค่าบูลีนที่ระบุว่าควรสร้างโปรไฟล์ผู้ใช้และตารางสมาชิกโดยอัตโนมัติหากไม่มีอยู่ มิ ฉะนั้นfalse
แม้ว่าค่าจริง จะ บ่งชี้ถึงการสร้างตาราง ฐานข้อมูลโดยอัตโนมัติ แต่ตัวฐานข้อมูลเองจะไม่ถูกสร้างขึ้นโดยอัตโนมัติ มันจะต้องมีอยู่
ฐานข้อมูลความปลอดภัยของเว็บ
ตารางUserProfileประกอบด้วยหนึ่งระเบียนสำหรับผู้ใช้แต่ละราย โดยมี ID ผู้ใช้ (คีย์หลัก) และชื่อผู้ใช้ (อีเมล):
UserId | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
ตารางการเป็น สมาชิก จะมีข้อมูลสมาชิกว่าเมื่อใดที่ผู้ใช้ถูกสร้างขึ้นและ (และเมื่อใด) ที่สมาชิกได้รับการยืนยัน
มีลักษณะเช่นนี้ (บางคอลัมน์ไม่แสดง):
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 |
การกำหนดค่าสมาชิกอย่างง่าย
คุณอาจได้รับข้อผิดพลาดโดยใช้อ็อบเจ็กต์ WebSecurity หากไซต์ของคุณไม่ได้รับการกำหนดค่าให้ใช้ระบบสมาชิก ASP.NET Web Pages SimpleMembership
กรณีนี้อาจเกิดขึ้นได้หากเซิร์ฟเวอร์ของผู้ให้บริการโฮสต์ได้รับการกำหนดค่าแตกต่างจากเซิร์ฟเวอร์ในเครื่องของคุณ ในการแก้ไขปัญหานี้ ให้เพิ่มองค์ประกอบต่อไปนี้ในไฟล์ Web.config ของไซต์:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>