[Net] talking about ASP. NET permission management and user verification

Source: Internet
Author: User

Introduction to ASP. NET permission management and user verification

During website construction, the most common user management and operation permission settings are verified. Although this is a common management operation, it is not easy to complete it. I recently referred to many. net books, especially those who care about this, and found out the appropriate user management methods based on their own characteristics, and completed the basic management library.
  
The user management method is designed at level 3 of "user-role-license". This is the user management method of the Windows operating system. A user can have multiple roles and one role can have multiple licenses.
  
Therefore, when designing databases, design them separately:
  
User table: Set userid, user name, user information
Role table: Set roleid, categoryid, rolename, and description
User Role table: userid and roleid
  
Category Table: categoryid, categoryname, and description
License table: permissionid, categoryid, permissionname, description
Role license table: roleid and permissionid
  
After the database is designed, users, roles, and licensed classes can be designed. The class library is divided into data-layer classes to complete creation, query, deletion, and update operations. Classes at the business layer need to implement data-independent functions, such as a list of user roles, licenses, and verification methods.
  
There is an important method at the business layer, that is, using. Net authentication methods. Therefore, two classes are created:
  
The first is the siteidentity class, which inherits from the business layer base class and uses the system. Security. Principal. iidentity interface. This class stores basic user information, including the user name, email, ID, and password. The method is simple. After verification, the basic information of the user is taken.
  
The second is the helloseaprincipal class, which also inherits the business base class and adopts the system. Security. Principal. iprincipal interface. The main methods of this class are verification classes, and the verified user information, user role list, and license list are stored in the class. This class has two methods and one property is required by the iidentity interface, namely isinrole, haspermission, and system. Security. Principal. iidentity identity.
  
After the class library is designed, it can be used to verify the user. For example, on the management logon page, you need to determine whether the user's password is correct and whether the user has permission to enter the management page.
  
The following is a Verification Section:
  
Helloseaprincipal admin = helloseaprincipal. validatelogin (txtadminname. Text, txtpassword. Text );

If (Admin = NULL ){

Labinfo. Text = "This Administrator account does not exist, or the entered password is incorrect. ";

} Else if (! Admin. haspermission (1 )){

Labinfo. Text = "you are not a management role and have no management permission. You cannot enter the system. ";

} Else {

Context. User = admin;

Formsauthentication. setauthcookie (txtadminname. Text, true );

Response. Redirect ("adminindex. aspx ");

}

Note :! 1 of admin. haspermission (1) is the ID number allowed in the database
  
You can use:
  
Helloseaprincipal currentprincipal = (helloseaprincipal) Context. user;

If (! Currentprincipal. haspermission (1 )){

}
This method is used to verify the user. Although it also uses cookies, the cookies in. NET are encrypted to achieve high security.

This design also has a disadvantage, that is, the License ID is a fixed value. Considering that the permission required for the general management page is relatively fixed, you can set the permission in the config file. Some channels and other verification licenses that need to change frequently are placed in the channel table of the database. The following describes how to read and write data in the config file. Reading and Writing from config files can be achieved through XML serialization and deserialization. classes can be used for encapsulation, which is very convenient to use, the following statement reads the permission by encapsulating the class:
  
If (! Admin. haspermission (operatepermissionconfig. getsettings (). managerlogin )){}
Here we will briefly introduce permissions and verification. Because the implementation code is very long, we only introduce a way of thinking here. The specific implementation has its own characteristics.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.