Hide Menu Items Based On Roles

B

beaudetious

I'm using a Menu web control in an ASP.NET 2.0 web page and I'd populating it
declaratively (sp?) in the web form itself. One of my menuitem's is only for
admin users to use. I'd like to learn how to hide this menu item when an
admin user is not logged in. Or better yet, display it when an admin user is
logged in. Either way.

I'm not sure which event to use and how to go about this. My attempts have
either caused exceptions all over the place.

Thanks,

Brian
 
P

Phillip Williams

Hi Brian,

If you turn the StaticMenuItem collection (that you have declaratively
defined on the web form) into a web.sitemap databound to the Menu control by
a SiteMapDataSource, and define the URL authorization rules in the web.config
then you can have the site map provider filters the site map nodes based on
the user's role setting by turning the SecurityTrimmingEnabled property value
to true.

To see this in a demo look at the QuickStart tutorials:
http://www.asp.net/QuickStart/aspnet/doc/navigation/sitenavapi.aspx#security

Also the Personal Web Site Starter Kit in VS.Net produces a demo site that
uses the same strategy for displaying menus.
 
B

beaudetious

The problem with that approach is that I'm already using the SiteMap for
breadcrumbs with a SiteMapPath control. I'll have to play around with this
idea to see if I can find a site layout that will work for both my menu and
my breadcrumbs. Ideally, I'd love to use different sitemaps, but that could
get unweildy in the long run.

Thanks,

Brian
 
S

sloan

You may want to implement your own custom SiteMapProvider.

Here is a hint to get you started:
public class MySecureMapProvider : XmlSiteMapProvider

{

public MySecureMapProvider ()

{

}



public override bool IsAccessibleToUser(HttpContext context, SiteMapNode
node)

{

//return base.IsAccessibleToUser(context, node);

string currentURL = node.Url;



if (currentURL.Length > 0)

{

return false;//PLACE BUSINESS RULE HERE...

}

return true;





}

}



<siteMap defaultProvider="XmlSiteMapProvider">

<providers>

<add name="XmlSiteMapProvider" type="MySecureMapProvider"
siteMapFile="~/Web.sitemap" securityTrimmingEnabled="true"/>

</providers>

</siteMap>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top