asp.net
Website Construction, will encounter the most commonly used user management and Operation permission set verification, although this is a very common management operation, but it is not easy to do perfect. A lot of references have been made recently. NET aspects of the book, particularly concerned about this aspect, and combined with their own characteristics, groping a bit for their own user management methods, completed the basic Management class library.
The user management method, at design time take "user--role-license" Level three, this is the Windows operating system user management method. A user can have multiple roles, and a single role can have multiple licenses.
So the database design is designed separately:
User table: Set userid, username, user information
Role table: Set Roleid, CategoryID, RoleName, Description
User Role table: UserID, Roleid
Classification table: CategoryID, CategoryName, Description
License list: PermissionID, CategoryID, Permissionname, Description
Role License table: Roleid, PermissionID
After you have designed the database, you can engage in designing users, roles, and licensed classes. Class Library data layer class, complete the establishment, query, delete, update and other operations. Classes in the business layer need to implement data-independent functions, such as user-owned roles, licensed lists, authentication methods, and so on.
In the business layer there is a very important way, that is, to use. NET is implemented, so two classes are established:
One is the Siteidentity class, which inherits from the base class of the business layer and uses the System.Security.Principal.IIdentity interface. This class mainly holds the user's basic information, including user name, email and ID number, password. It's a simpler method to pass the user's basic information after verification.
The second is Helloseaprincipal class, it also inherits the business base class, uses the System.Security.Principal.IPrincipal interface. The primary method for this class is to validate the class and store the authenticated user information, user role list, and license list in the class. There are two methods and one property in this class that are required by the IIdentity interface, namely IsInRole, Haspermission, and System.Security.Principal.IIdentity Identity.
Once the class library is designed, it can be used to authenticate users. For example, in the admin login page, you need to determine whether the user's password is correct and determine whether the user has permission to enter the Administration page.
The following is a section of the validation:
Helloseaprincipal admin = Helloseaprincipal.validatelogin (Txtadminname.text, Txtpassword . Text);
if (admin = null) {
Labinfo.text = "There is no administrator account, or the password is incorrect. ";
else if (!admin. Haspermission (1)) {
Labinfo.text = "You are not an administrative role and you do not have administrative permission to enter this system." ";
} else {
Context.User = admin;
Formsauthentication.setauthcookie (Txtadminname.text, true);
Response.Redirect ("adminindex.aspx");
}
Note:!admin. Haspermission (1) of 1 is the license ID number in the database
In the internal page you can use:
Helloseaprincipal CurrentPrincipal = (helloseaprincipal) Context.User;
if (!currentprincipal.haspermission (1)) {
}
This method validates the user, although it is also through cookies, but the cookies in. NET are encrypted and can achieve high security
This design also has the disadvantage that the license ID number is a fixed value. The license is set in the config file, taking into account that the General Administration page requires a relatively fixed license. And some channels, such as the need for frequent changes in the validation license placed in the Database channel table. Here is the implementation of the set method in the config file I used, not detailing how to read and write. Reading and writing from config files is easy to implement with XML serialization and drag, can be encapsulated with classes, and is convenient to use, the following statement is the way to read permissions through the Encapsulation class:
if (!admin. Haspermission (Operatepermissionconfig.getsettings (). Managerlogin)) {}
About permissions and authentication, introduced here, can be simply introduced. Because the implementation of the code is very long, so here is only to introduce a way of thinking, concrete implementation of each person will have their own characteristics.