ASP. NET Site Maps, Security Trimming and Roles

Source: Internet
Author: User

This is one of the most frequently asked questions and seems a constant source of confusion for everyone, as it was for me when I first read about it. the ASP. NET SiteMap allows a navigational structure to be defined as a set of XML elements, which are perfect for describing a hierarchy of menu items. these XML items are a siteMapNode element, which has an attribute roles. it seems obvious that this defines the roles that can see this item, but the obvious is in fact wrong. here is the most important fact about site maps:

The roles attribute does not restrict visibility of a node.

That shoshould be clear enough, even if it still seems wrong. Here's how it works.

All restriction to pages is handled via authorization. you can do this either in the base web. config, or in web. config files in folders. for example, assume there is an Admin folder, under which all the administration pages are kept. you only want these pages accessible to users within the Admin role. you wowould configure your authorization like so:

 

Code
<Location path = "Admin">
<System. web>
<Authorization>
<Allow roles = "Admin"/>
<Deny users = "*"/>
</Authorization>
</System. web>
</Location>

 

The Admin folder can now no longer be accessed by anyone who is not in the Admin role; if you aren't in the Admin role and try to navigate to a page in the Admin folder, either via link on another page or by typing the URL directly into the browser, you'll be redirected to the login page. you can have multiple location elements in your web. config, for different folders or even individual files; in fact if you have a restrictive site, you may want to explicitly open up certain pages, such as the login page; it's hard to login to a site when you don't have authorization to access the login page. if you prefer not to clutter your base web. config you can create a web. config file in the Admin folder with the same rules; you won't need the location element since the configuration applies to the current folder.

So that's authorization done; access to the pages is locked down. now lets consider navigation. the ASP. NET navigation framework honors the authorization, but only if you configure security trimming on the provider, which isn't configured by default. this means that you need to add the site map configuration to web. config:

 

Code
<SiteMap enabled = "true" defaultProvider = "AspXmlSiteMapProvider">
<Providers>
<Clear/>
<Add name = "AspXmlSiteMapProvider"
SecurityTrimmingEnabled = "true"
Type = "System. Web. XmlSiteMapProvider, System. Web, Version = 2.0.3600.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"
SiteMapFile = "web. sitemap"/>
</Providers>
</SiteMap>

Most of this is configured at the machine level when ASP. NET is installed, but crucially the securityTrimmingEnabled value is set to false by default. what the above does is clear out the existing configuration and add a new entry with the attribute set to true. at this stage the navigation framework will now honor the authorization rules, so menu items won't be shown if the user doesn't have authorization for that item; it doesn't matter if you use a Menu or TreeView to display the menu items, the crucial part is using the SiteMapDataSource (or the Sitemap API if you're building the menu manually ). if you have a custom site map provider, such as a database driven one (such as this one on MSDN), then this might have to do it's own security checking, but it depends at which base class you inherit from. that's another story for another post though.

So if you don't need to modify the site map elements themselves, what's the roles attribute? Well this works in the opposite way you probably construct CT, by opening up visibility of the node, showing the node if the user is in the stated role even if they don't have authorization to access the page itself (because the authorization rule restrict them from accessing it ). why wocould you do this? Well, you have to understand how security trimming works. when deciding whether a user can see a node, both the authorization and the physical file permissions are checked; if either fail then the node is deemed inaccessible. there are two very common times when physical file checks fail:

The URL isn' t local. If the file doesn' t exist locally then no check can take place.
There isn't a URL. The node cocould be just a container node, with child pages but no page itself.
In both of these cases the physical file checks fail so the node won't be shown. You therefore may need to open up the visibility of the node. For example, consider the following:

 

Code
<SiteMapNode title = "Admin" roles = "Admin">
<SiteMapNode url = "~ /Admin/membership_CreateMember.aspx "title =" Create User "/>
<SiteMapNode url = "~ /Admin/membership_GetUsers.aspx "title =" View Users "/>
<SiteMapNode url = "~ /Admin/roleManager_CreateRole.aspx "title =" Create Role "/>
<SiteMapNode url = "~ /Admin/roleManager_AddUserToRole.aspx "title =" Add User to Role "/>
</SiteMapNode>

 

Here the Admin node doesn't have a physical page, it's purely to allow organization of the admin items into their own submenu. without the additional roles attribute the node and children wouldn't appear, but roles = "Admin" states that the node shoshould also be shown to users within the Admin role, even if the security checking fails. we don't need the attribute on the child nodes because they have physical pages, so the file checks will succeed.

So it's fairly straightforward if you remember the rules:

Configure security restrictions on pages with authorization in web. config.
Redefine the site map provider, enabling security trimming.
Add the roles attribute to site map nodes to widen the visibility.

Here is the original link, http://blogs.ipona.com/davids/archive/2009/01/12/8554.aspx

 

 

Related Article

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.