This article describes how to configure membership and ASP. NET Forms authentication.
After all the basic configurations are complete, we also need to configure which directories are allowed to be accessed anonymously and which are pages that need to be accessed after logon.
First, create an admin folder in the project, add a web. config file to the admin folder, and add the following content under the <system. Web> nodeCode:
< Authorization >
< Allow Users = "Admin" />
< Deny Users = "*" />
</ Authorization >
Then, add a page under the Admin directory, visit the page, and check the effect. If you have correctly configured all the content as described in the previous article, you will find that the page is not displayed, but jumps toAuthentication> Lower <Forms> Log on to the page pointed to by the node's loginurl attribute! Isn't that what we need?
Now let's explain the meaning of the above Configuration:
<Allow> node: As the name implies, access is permitted,<AllowUsers= "Admin"/>Is to allow users with the username "admin" to access.
<Deny> node: access is prohibited. Here, a wildcard"*", The wildcard has two: * represents all users, and a"?" Represents all anonymous users. So<DenyUsers= "*"/>This means that access from all users is not allowed. Of course, if it is:<DenyUsers= "? "/>This means that access by anonymous users is not allowed.
It should be noted that all configurations are matched in the order from top to bottom. Once the matching is successful, the matching will not be performed downward. For example:
< Authorization >
< Allow Users = "Admin" />
< Allow Users = "Zhangsan" />
< Deny Users = "*" />
</ Authorization >
ASP. NET first checks the current Login User Name = "admin "? If the value is equal to, the user is not allowed to access the database. If the value is not the same as that, the user name = "zhangsan" for the current logon is determined to be equal to that of the current logon user. If the value is not the same, access is permitted. If the value is not the same, access is directed down and read<DenyUsers= "*"/>This configuration rejects access by all users and redirects to the page specified by loginurl and requires logon again.
However, in this case, only permission control at the directory level can be implemented. What should we do if we want to control the access permission of a file? Do you have to put this file in a folder and then add web. config for control? The answer is no. For access control of a single file, ASP. NET also has the corresponding Configuration:
< Configuration >
< Location Path = "A. aspx" >
< System. Web >
< Authorization >
< Deny Users = "? " />
</ Authorization >
</ System. Web >
</ Location >
< System. Web >
< Authorization >
< Allow Users = "Admin" />
< Allow Users = "Zhangsan" />
< Deny Users = "*" />
</ Authorization >
</ System. Web >
</ Configuration >
Based on the above configuration, we can go to the <system. Web> node (Must be above) Add another <location> node, and use the path attribute to specify which file the configuration in the location is. I will not talk much about the specific content. I think everyone can understand it. Note that:There can be multiple location nodes. This means that different files in the same directory can have different access permissions.
Now, more than half of the configuration has been completed. Another problem is: if we have many users, we need to list all these users in the configuration file. It will be too troublesome to add new users and modify the configurations later! What should I do? A general approach is to introduce the role concept. Assign a role to all users, such as users and Admin. Then we only need to control the access permissions of these roles. After adding a new user, you only need to assign a role to the new user, instead of modifying the configuration. This is really convenient. In fact, membership also provides the role concept, which can be implemented simply by simple configuration.
To implement the role function, it is very simple. We still need to go to machine. <system. web> Find the <rolemanager> node under the node, and then the entire node is copied to the Web. in config, there are usually two <add> subnodes. we can delete one and leave one. The content is as follows:
< Rolemanager >
< Providers >
< Add Name = "Aspnetsqlroleprovider"
Connectionstringname = "Localsqlserver"
Applicationname = "/"
Type = "System. Web. Security. sqlroleprovider, system. Web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" />
</ Providers >
</ Rolemanager >
This configuration is relatively simple. The configuration is the same as the previous configuration. Slightly modify:
< Rolemanager Enabled = "True" Defaultprovider = "Myaspnetsqlroleprovider" >
< Providers >
< Add Name = "Myaspnetsqlroleprovider"
Connectionstringname = "Connectionstring"
Applicationname = "Testmembership"
Type = "System. Web. Security. sqlroleprovider, system. Web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" />
</ Providers >
</ Rolemanager >
Note: This rolemanager has an extra attribute enable = "true". This is because role management is disabled by default, so we must set this attribute to enable.
All the configurations are complete here. For details about the future, refer to ASP. NET authentication mechanism membership-encoding.