Generate a matched menu in. Net Based on the page's role authentication method

Source: Internet
Author: User

I saw that many of them are themselvesProgramAfter obtaining the role permissions of a user, you can determine and compile corresponding function 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:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Sitemap Xmlns = "Http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"   >
< Sitemapnode URL = "~ /Default. aspx" Title = "Homepage" Description = ""   >
< Sitemapnode URL = "~ /Register. aspx" Title = "Registration" Description = ""   />
< Sitemapnode URL = "~ /Login. aspx" Title = "Login" Description = ""   />
< Sitemapnode URL = "~ /Album/default. aspx" Title = "Personal Management" Description = "" >
< Sitemapnode URL = "~ /Album/albummanager. aspx" Title = "Album management" Description = ""   />
< Sitemapnode URL = "~ /Album/ImageUploader. aspx" Title = "Image Upload" Description = ""   />
< Sitemapnode URL = "~ /Album/imagemanager. aspx" Title = "Image management" Description = ""   />
</ Sitemapnode >
</ Sitemapnode >
</ 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.

< System . Web >
< Authorization >
<! -- The current directory is accessible only by users of the album role. -->
< Allow Roles = "Album"   />
< Deny Users = "*" />
</ Authorization >
</ System. Web >

< Location Path = "ImageUploader. aspx" >
< System . Web >
< Authorization >
<! -- ImageUploader. aspx can be accessed only by users with the blog role. -->
< Allow Roles = "Blog" />
< Deny Users = "*" />
</ Authorization >
</ System. Web >
</Location>

3. Enable the site map role filter configuration in the root web. config. To save trouble, copy and paste the configuration directly. < System . Web >
< Sitemap Defaultprovider = "Xmlsitemapprovider" Enabled = "True" >
< Providers >
< Add Name = "Xmlsitemapprovider"
Description = "Default sitemap provider ."
Type = "System. Web. xmlsitemapprovider"
Sitemapfile = "Web. sitemap"
Securitytrimmingenabled = "True"   />
</ Providers >
</ Sitemap >
</ System. Web >

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:(Before login, only the files that can be accessed by anyone in the root directory are displayed.)

Test Case 2:(After logging in, you must have the album role but not the blog role.)

Test Case 3:(After logging in, you must have the album role and the blog role.)

After finishing the work, go home. Obviously, this method is greatly reduced.CodeAnd it is very convenient to bind the page file or directory with the role.
I want to talk about it more, but mm is ill at home, so let's talk about it tomorrow!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.