صفحات ويب ASP.NET - كائن أمان الويب
وصف
يوفر كائن WebSecurity الأمان والمصادقة لتطبيقات صفحات ويب ASP.NET.
باستخدام كائن WebSecurity ، يمكنك إنشاء حسابات مستخدمين ، وتسجيل دخول وخروج المستخدمين ، وإعادة تعيين كلمات المرور أو تغييرها ، والمزيد.
مرجع كائن 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).
"ملف تعريف المستخدم" هو اسم جدول قاعدة البيانات الذي يحتوي على معلومات ملف تعريف المستخدم.
"معرف المستخدم" هو اسم العمود الذي يحتوي على معرفات المستخدم (المفتاح الأساسي).
"البريد الإلكتروني" هو اسم العمود الذي يحتوي على أسماء المستخدمين.
المعلمة الأخيرة true هي قيمة منطقية تشير إلى أنه يجب إنشاء ملف تعريف المستخدم وجداول العضوية تلقائيًا إذا لم تكن موجودة ، وإلا فهي خطأ .
على الرغم من أن القيمة true تشير إلى الإنشاء التلقائي لجداول قاعدة البيانات ، فلن يتم إنشاء قاعدة البيانات نفسها تلقائيًا. يجب أن تكون موجودة.
قاعدة بيانات WebSecurity
يحتوي الجدول UserProfile على سجل واحد لكل مستخدم ، مع معرف المستخدم (المفتاح الأساسي) واسم المستخدم (البريد الإلكتروني):
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 SimpleMembership .
يمكن أن يحدث هذا إذا تم تكوين خادم موفر الاستضافة بشكل مختلف عن الخادم المحلي الخاص بك. لإصلاح ذلك ، أضف العنصر التالي إلى ملف Web.config الخاص بالموقع:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>