บทช่วยสอน ASP

ASP HOME

กวดวิชา WP

แนะนำหน้าเว็บ มีดโกนหน้าเว็บ เค้าโครงหน้าเว็บ โฟลเดอร์หน้าเว็บ หน้าเว็บทั่วโลก แบบฟอร์มหน้าเว็บ วัตถุหน้าเว็บ ไฟล์หน้าเว็บ ฐานข้อมูลหน้าเว็บ ผู้ช่วยหน้าเว็บ หน้าเว็บ WebGrid แผนภูมิหน้าเว็บ อีเมลหน้าเว็บ ความปลอดภัยของหน้าเว็บ เผยแพร่หน้าเว็บ ตัวอย่างหน้าเว็บ ชั้นเรียนของหน้าเว็บ

มีดโกน ASP.NET

แนะนำมีดโกน มีดโกนไวยากรณ์ มีดโกน C# ตัวแปร มีดโกน C# ลูป มีดโกน C# Logic ตัวแปรมีดโกน VB มีดโกน VB Loops มีดโกน VB Logic

ASP Classic

แนะนำ ASP ไวยากรณ์ ASP ตัวแปร ASP ขั้นตอน ASP เงื่อนไข ASP ASP วนรอบ แบบฟอร์ม ASP คุกกี้ ASP เซสชัน ASP แอปพลิเคชัน ASP ASP #รวม ASP Global.asa ASP AJAX อีเมล ASP ตัวอย่าง ASP

การอ้างอิง ASP

ฟังก์ชัน ASP VB คีย์เวิร์ด ASP VB การตอบสนอง ASP คำขอ ASP แอปพลิเคชัน ASP เซสชัน ASP เซิร์ฟเวอร์ ASP ข้อผิดพลาด ASP ASP FileSystem ASP TextStream ไดรฟ์ ASP ไฟล์ ASP โฟลเดอร์ ASP พจนานุกรม ASP ASP AdRotator ASP BrowserCap การเชื่อมโยงเนื้อหา ASP ตัวหมุนเนื้อหา ASP ASP Quick Ref

กวดวิชา ADO

แนะนำ ADO ADO Connect ชุดระเบียน ADO จอแสดงผล ADO แบบสอบถาม ADO ADO Sort เพิ่ม ADO ADO Update ADO ลบ สาธิต ADO ADO เร่งความเร็ว

วัตถุ ADO

คำสั่ง ADO การเชื่อมต่อ ADO ข้อผิดพลาด ADO ADO Field พารามิเตอร์ ADO ADO พร็อพเพอร์ตี้ บันทึก ADO ชุดระเบียน ADO ADO สตรีม ประเภทข้อมูล ADO

หน้าเว็บ 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 Email
1 [email protected]
[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>