Role Control and Management
• Authentication and authorization mechanisms
• Use user management controls
• Member qualifications and role management
I. ASP. NET 2.0 Security
Necessity
For websites, user identity authentication and permission management are very important.
The user name and password are used to authenticate the user and assign the user the resources he can access. This part of work has always been an important part of website development.
In other cases, you need to identify permissions based on the user's identity. Different users need to display different content when accessing the same page.
This involves user permission management and is also the core content of website development.
Ii. ASP. NET 2.0 role control Overview
• ASP. the membership and rolemanager of NET 2.0 can effectively solve this problem. They can not only centrally manage user login information, but also manage user permissions by category, allows developers to easily set website permissions and security.
• The ASP. NET 2.0 Login control provides a very convenient method for building Login and user management information.
Iii. authentication and authorization
• ASP. NET performs authorization management by working with IIS. Two authentication methods
1. query the acls list or license to determine whether the access has the right to browse.
2. URL Authentication
When a user accesses a website. The two authorization Methods perform different actions respectively.
The first authentication method determines the system account specified by the user based on the user's login information, and then determines whether the system account has access permissions to the requested local resources.
The second authentication method performs authorization authentication by checking the asp.net configuration file.
• In Asp.net's page authentication method, you can use the following three methods for identity authentication.
• You can configure different authentication methods by modifying the authentication attribute in the config file.
1. Use the PassPort SDK for Secondary Development of Passport
2. cookie-based Identity Authentication Mechanism Forms
3. Based on windows authentication, first determine the identity and group of windows users
4. No authorization or authentication None
Description Value
<Configuration>
<System. web>
<Authentication mode = "Forms"/>/forms mode
</System. web>
</Configuration>
1. Windows authentication method
• Window authentication method used
The windowsprincipal Class determines the user's windows identity, and then authenticates the user based on the user's windows identity group.
• Add <authentication mode = "Windows"/> to wen. config.
2. Form authentication method
• The from authentication method is to provide the user's input ID and password in the form, and perform identity authentication based on the user's input ID and password.
• The form authentication method also uses cookies to record user information. When a user accesses other pages, the program accesses cookies to obtain user identity information.
Form authentication configuration file
<Configuration>
<System. Web>
<Authentication mode = "forms"/> <authorization>
<Forms name = ". aspxcookiedemo"
Loginurl = "login. aspx" Protection = "all" timeout = "30"
Path = "/">
<! -- Protection = "[All | none | encryption | validation]" -->
</Forms>
<Deny users = "? "/>
</Authorization>
</System. Web>
</Configuration>
• Attribute meanings in the configuration file are shown in the following table:
Protection: how cookies are saved
Path: Cookie storage path
Timeout: the default cookie survival time is 30 minutes.
Name: Name of the Cookie. Note that if a server has many applications, you must give the cookie a different Name.
LoginUrl: specify a page for login
Protection attributes
• Protection is used to describe how cookies are stored. You can select the following four items:
All -- Encryption and Validation
Validation-monitors cookies to ensure that they are not monitored or tampered with during transmission. However, cookies are not encrypted.
Encryption-use des or layer-3 des to encrypt cookies, but do not monitor whether cookies are being monitored or tampered with during transmission.
None -- do not use any method to protect cookies
Use a file to record the user's account and password
• Users can also specify accessible user names and passwords.
• <Authentication>
• <Credentials passwordFormat = "SHA1">
• <User name = "Mary"
Password = "94f85995c7425eec546c321821aa4beca
9A3E2B1 "/>
• <User name = "John"
Password = "5753A498F025464D72E088A9D5D6E87259
2D5F91 "/>
• </Credentials>
• </Authentication>
• You can specify the password storage mode when specifying the Password Storage Mode. There are three methods. See the following table.
Use MD5 for MD5 Encryption
Use SHA1 for SHA1 Encryption
Store Clear without encryption
• User access can also be assigned to user roles by customizing access rules.
• <Authorization>
• <Allow users = "someone@www.frontfree.com"/>
• <Allow roles = "Admins"/>
• <Deny users = "*"/> // disable all users. If yes? Indicates that anonymous users are disabled.
• </Authorization>
• The code above specifies that only a someone@www.frontfree.com user can access the site and that the user has the permission to be an administrator
• In web. config, you can also set the access user Id and access method by configuring the all and deny attributes.
• <Allow VERB = "POST" users = "John, Mary"/>
• <Deny VERB = "POST" users = "*"/>
• <Allow VERB = "GET" users = "*"/>
• Form authentication demonstration
Use user management controls
• Login, Loginstatus, and CreateUserWizard controls
Example
• LoginView example
• ChangePassword example
Iv. Member Management
• Member management features are based on membership and menbershipuser. You can use the membership class to create users for asp.net.
• The membership class can also do the following:
Create a new membershipuser
User identity authentication
Retrieve a membershipuser instance
Update a membershipuser instance
Search for a user based on different conditions
Obtain the current number of online users
Delete an account that is no longer needed
Access the attributes of a membership
Retrieve a user's password
Change the password of a user
Change the password and answer of a user
Unlock a user that has been locked due to multiple failed attempts.
Role management
• Role management is implemented based on the role class.
• Through role management, you can do the following:
• Create a role
• Delete a role
• Assign roles to a user
• Remove a User Role
• Determine whether a user is authorized to a special role
• Find a user in a role
• Obtain the role information of a user from the user information
1. User Management
Create user
• By calling the createuser method of membership,
Users can be created. Note that the membership password must be longer than 7 characters long and contain at least one special character.
User Login and access user attributes
• Use the validateuser method in membershiip to check whether the user is valid.
• Membership also provides the getuser method to return a membershipuser class to obtain user attributes.
• User Login
• User Account Information Display
Update user attributes
• By combining dataview with a membership, you can easily modify user registration information.
• Demo of changing user information
Unlock
• When a user attempts to use a large number of passwords, the system will lock the user's account. You can use the islockout attribute of membershipuser to determine whether the user is locked. If the user is locked, you can use the unlockuser method to unlock the lock.
• Unlock users
Delete a user
• You can call the deleteuser method of Membership to delete a user. The return value is used to determine whether the deletion is successful.
• Delete a user
2. role management
• Role management is a management method that authenticates user identities and permissions. The following examples Use Role Management
• Add and delete roles
• By Using roles's createrole and deleterole methods, we can add and delete roles and call
Configure role attributes for users
• You can use the addusertorole and removeuserfromrole methods of the roles class to specify a role or remove a role.
Use rolemanage to authorize a page
• You can also use role manage to grant page authorization to users of a specified role.
• Configure in web. config
• User authorization Page Test