Monday, August 30, 2010

Manage the content of navigation controls based on the roles defined in ASP.NET

This one is really cool! I suppose you have already set up role and membership in your ASP.NET application. If you have a TreeView or a Menu control that uses SiteMapDataSource and in the Web.sitemap file, you have elements pointing to files in the folders the access rule of which you have set, it makes sense if you want to show and hide the menu or treeview items (nodes) according to the user’s roles. This can be accomplished by a setting added to the web.config file:

<siteMap defaultProvider="default">
      <providers>
            <clear/>
            <add name="default" type="System.Web.XmlSiteMapProvider"
securityTrimmingEnabled="true" siteMapFile="Web.sitemap" />
      </providers>
</siteMap>

As you can see, the magic is done by an attribute called securityTrimmingEnabled.

No comments:

Post a Comment