After reading that many people use their own programs to obtain the role permissions of users and then judge and write corresponding functional menus, in fact,. net allows developers to combine these functions themselves;
There is a lot of such information on the Internet, but I feel that there are too many lectures, so I can simply summarize them for you!
Note: the site must be in the asp.net Standard Authentication mode. For custom cookie authentication, the custom Session mode can be cool!
Conditions
1. Prepare SiteMap for the site
Create a Web. sitemap configuration file in the root directory of the site, for example:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <siteMap xmlns = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
3 <siteMapNode url = "~ /Default. aspx "title =" Homepage "description =" ">
4 <siteMapNode url = "~ /Register. aspx "title =" Register "description =" "/>
5 <siteMapNode url = "~ /Login. aspx "title =" Login "description =" "/>
6 <siteMapNode url = "~ /Album/Default. aspx "title =" personal management "description =" ">
7 <siteMapNode url = "~ /Album/AlbumManager. aspx "title =" Album management "description =" "/>
8 <siteMapNode url = "~ /Album/ImageUploader. aspx "title =" Image Upload "description =" "/>
9 <siteMapNode url = "~ /Album/ImageManager. aspx "title =" image management "description =" "/>
10 </siteMapNode>
11 </siteMapNode>
12 </siteMap>
Note: The first-level node can have only one node, and other nodes are free of choice. You can write all the pages related to the entire site, or write only the pages related to permission control, this can be inherited.
If you have MSDN2005 installed, you can see this content: ASP. NET site map.
Ms-help: // MS. VSCC. v80/MS. MSDN. v80/MS. VisualStudio. v80.chs/dv_aspnetcon/html/6b85a558-1df8-44cf-bea6-62e61bcc8d1_htm
2. Set access permissions for files or directories
You can set it in the Web. config file of the relevant directory.
1 <system. web>
2 <authorization>
3 <! -- The current directory is accessible only by users of the Album role -->
4 <allow roles = "Album"/>
5 <deny users = "*"/>
6 </authorization>
7 </system. web>
8
9 <location path = "ImageUploader. aspx">
10 <system. web>
11 <authorization>
12 <! -- ImageUploader. aspx can be accessed only by users with the Blog role -->
13 <allow roles = "Blog"/>
14 <deny users = "*"/>
15 </authorization>
16 </system. web>
17 </location>
3. Enable the site map role filter configuration in the root Web. config. To save trouble, copy and paste the configuration directly. 1 <system. web>
2 <siteMap defaultProvider = "XmlSiteMapProvider" enabled = "true">
3 <providers>
4 <add name = "XmlSiteMapProvider"
5 description = "Default SiteMap provider ."
6 type = "System. Web. XmlSiteMapProvider"
7 siteMapFile = "Web. sitemap"
8 securityTrimmingEnabled = "true"/>
9 </providers>
10 </siteMap>
11 </system. web>
12
That's it! Test it ~~!
1. Drag a Menu control to the page and create a new data source of the "Site Map" type. The name is random. Click OK to configure it!
Test Case 1: (only files accessible to anyone in the root directory are displayed before logon)
Test Case 2: (After login, the Album role is available, but the Blog role is not available)
Test Case 3: (After login, the Album role and the Blog role are available)
After finishing the job, I went home. Obviously, this method reduces a lot of code and binds the page file or directory to the role, which is very convenient.