Hide Menu Items Based On Roles

Discussion in 'ASP .Net Web Controls' started by beaudetious, Jan 8, 2006.

  1. beaudetious

    beaudetious Guest

    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
    beaudetious, Jan 8, 2006
    #1
    1. Advertising

  2. 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.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "beaudetious" wrote:

    > 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
    Phillip Williams, Jan 8, 2006
    #2
    1. Advertising

  3. beaudetious

    beaudetious Guest

    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

    "Phillip Williams" wrote:

    > 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.
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "beaudetious" wrote:
    >
    > > 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
    beaudetious, Jan 8, 2006
    #3
  4. beaudetious

    sloan Guest

    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>





    "beaudetious" <> wrote in message
    news:...
    > 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
    sloan, Jan 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chicagoboy27
    Replies:
    1
    Views:
    321
    Chicagoboy27
    Dec 12, 2006
  2. Larry Bud
    Replies:
    13
    Views:
    7,644
    Larry Bud
    Jan 7, 2008
  3. Nilla
    Replies:
    3
    Views:
    1,001
    Nilla
    May 29, 2008
  4. Replies:
    4
    Views:
    2,148
  5. Chuck P

    Menu items and Roles

    Chuck P, Mar 7, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    514
    Steven Cheng[MSFT]
    Mar 8, 2007
Loading...

Share This Page