ASP.NET वेब पेज - वेब सुरक्षा वस्तु
विवरण
WebSecurity Object ASP.NET वेब पेज अनुप्रयोगों के लिए सुरक्षा और प्रमाणीकरण प्रदान करता है।
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 डेटाबेस बनाना या प्रारंभ करना होगा।
अपने वेब के मूल में, _AppStart.cshtml नाम का एक पृष्ठ बनाएं (या पृष्ठ संपादित करें) ।
फ़ाइल के अंदर निम्न कोड डालें:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile",
"UserId", "Email", true);
}
उपरोक्त कोड हर बार वेब साइट (एप्लिकेशन) शुरू होने पर चलेगा। यह WebSecurity डेटाबेस को इनिशियलाइज़ करता है।
"उपयोगकर्ता" वेब सुरक्षा डेटाबेस (Users.sdf) का नाम है।
"UserProfile" उस डेटाबेस तालिका का नाम है जिसमें उपयोगकर्ता प्रोफ़ाइल जानकारी होती है।
"UserId" उस कॉलम का नाम है जिसमें यूजर आईडी (प्राथमिक कुंजी) है।
"ईमेल" उस कॉलम का नाम है जिसमें उपयोगकर्ता नाम हैं।
अंतिम पैरामीटर सत्य एक बूलियन मान है जो दर्शाता है कि उपयोगकर्ता प्रोफ़ाइल और सदस्यता तालिकाएं स्वचालित रूप से बनाई जानी चाहिए यदि वे मौजूद नहीं हैं, अन्यथा झूठी ।
हालांकि सत्य डेटाबेस तालिकाओं के स्वचालित निर्माण को इंगित करता है , डेटाबेस स्वयं स्वचालित रूप से नहीं बनाया जाएगा। यह मौजूद होना चाहिए।
वेब सुरक्षा डेटाबेस
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 |
सरल सदस्यता विन्यास
यदि आपकी साइट ASP.NET वेब पेज सदस्यता प्रणाली SimpleMembership का उपयोग करने के लिए कॉन्फ़िगर नहीं है, तो आपको WebSecurity ऑब्जेक्ट का उपयोग करने में त्रुटियाँ मिल सकती हैं ।
यह तब हो सकता है जब एक होस्टिंग प्रदाता का सर्वर आपके स्थानीय सर्वर से अलग तरीके से कॉन्फ़िगर किया गया हो। इसे ठीक करने के लिए, साइट की Web.config फ़ाइल में निम्न तत्व जोड़ें:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>