SoFunction
Updated on 2025-03-07

Identity authentication (directory verification)

For example, a news system usually requires authentication only on the web page that publishes news, while users do not need authentication when browsing news. For this case, authentication is required for different web pages.
To implement this function, just modify the "Identity Authentication (Simplest Chapter)" slightly. The specific operation method is as follows:

The first step is to create a subfolder, place all web pages to be verified in this folder, and set the name of the folder to "admin".

The second step is to modify the file.
1. Find the <authentication> section in <> and </> and change it to "<authentication mode="Forms"><forms loginUrl="~/admin/"></forms></authentication>", where Forms represents the use of form authentication; loginUrl is used to specify the login page URL. I personally prefer to place the login page in the admin folder, so I need to specify the login page URL here. If I still want to use the page in the root directory as the login page, I can omit the <forms> section.
2. Add "<authorization><allow users="?"/></authorization>" to <> and </>, where "<allow users="?"/>" means that all anonymous users are allowed. Note the difference here from "Identity Authentication (Easiest Chapter)". In "Identity Authentication (Easiest Chapter)", "<deny users="?"/>" is used here to deny all anonymous users.

Step 3: Add a file to the subfolder that requires authentication, in this case, add the file in the "admin" folder.

Step 4: Modify the files in the subfolder that requires authentication, in this case the files in the "admin" folder.
In <> and </>, add "<authorization><deny users="?"/></authorization>". Since all anonymous users are allowed to access in the file in the root directory, "<deny users="?"/>" must be used to deny access to anonymous users in subfolders that cannot be allowed to access anonymous users. In addition, there may be no <authentication> section in the files in the subfolder.

Step 5: Create a file in the "admin" subfolder. If the <forms> section is not used in the second step to specify the user login page, create the file in the website root directory.

Step 6: Verify the identity in the file (or file). If the authentication is passed, use() to create an authenticated ticket for the user and add it to the cookie. In the future, if you visit other web pages under the admin subdirectory of the website, you will no longer need to use them for authentication. The code after clicking the submit button is similar to the code in "Identity Authentication (Easiest Chapter)", so I will not go into details here.

This example was tested and passed in VS2005.
Advantages of this example: the process and code are very simple. You can specify the web page in a certain directory for authentication, rather than all websites on the entire website for authentication.
Disadvantages of this example: the authentication is too simple, and there are only two types of verification results. One is that it does not pass the verification, so access is denied; the other is that it passes the verification and can be accessed. If you want to subdivide permissions, it is said that Administrator A has the added permissions, while Administrator B only has the view permissions, this function cannot be implemented.