asp.net authentication (sub-directory verification) _ Practical tips
Source: Internet
Author: User
A news system, for example, usually requires authentication only on the Web page where the news is posted, and the user does not need authentication to browse the news. For this kind of situation, it is necessary to authenticate the different Web pages.
To achieve this function, as long as the "ASP.net in the authentication (most simple article)" slightly modified, the specific mode of operation as follows:
First, create a subfolder, put all the pages you want to verify in this folder, and set the folder's name "Admin".
Step two, modify the Web.config file.
1, in <system.web> and </system.web> find <authentication> section, change it to "<authentication mode=" Forms "> <forms loginurl= "~/admin/adminlogin.aspx", where Forms representatives use form authentication Loginurl is used to specify the login page URL, and individuals prefer to place the login page in the Admin folder, so specify the login page URL, or omit the <forms> section if you want to use the Login.aspx page in the root directory as the login page.
2, in <system.web> and </system.web> add "<authorization><allow users="? /></authorization> ", where" <allow users= "?" /> "represents the permission for all anonymous users. Note the difference between this and the authentication (simplest article) in ASP.net, in the authentication (simplest article) of ASP.net, where the "<deny users=" is used? /> ", which is used to deny all anonymous users.
Step three, add a Web.config file in the subfolder that requires authentication, in this case, add the file in the "admin" folder.
Step fourth, modify the Web.config file in the subfolder that requires authentication, in this case, the Web.config file in the "admin" folder.
In <system.web> and </system.web>, add "<authorization><deny users="? /></authorization> ", because all anonymous users are declared in the Web.config file in the root directory, you must use the <deny users= in a subfolder that cannot allow anonymous users to access it?" /> "To deny anonymous user access. In addition, there can be no <authentication> section in the Web.config file in the subfolder.
Step Fifth, create the adminlogin.aspx file in the "admin" subfolder. If you do not use the <forms> section in the second step to specify a user login page, create the Login.aspx file in the root directory of the Web site.
Step sixth, verify the identity in the adminlogin.aspx file (or login.aspx file), and if authentication is passed, use Formsauthentication.setauthcookie () to create an authenticated ticket for the user. and add it to the cookie. Later, you can access other pages in the admin subdirectory of the site, and you don't need to use authentication. The code after clicking the Submit button is similar to the code in the authentication (simplest article) in asp.net and is no longer discussed here.
This example tests through in VS2005.
The advantage of this example is that procedures and code are simple enough to specify that a Web page in a directory is authenticated, not all sites on the entire site are authenticated.
The disadvantage of this example is that authentication is too simple, the result of authentication is only two, one is not authenticated, thus denying access, one is through authentication, can access. If you want to subdivide the permissions, the comparison says that administrator A has added permissions, and Administrator B has only view permissions, and this functionality is not possible.
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