How to hide role-based menu items in ASP.Net

I am trying to hide an item from menu items if the user is not in the administrator role. Here is the code that I use in the code in my site host, but the code does not hide the admin page in which I am trying to hide it. Here is the HTML markup for the home page:

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> <Items> <asp:MenuItem NavigateUrl="~/Admin/Authors.aspx" Text="Admin" /> <asp:MenuItem NavigateUrl="~/Users/KCA_Summary.aspx" Text="KCA Summary" /> <asp:MenuItem NavigateUrl="~/Users/Details.aspx" Text="KCA Details" /> </Items> </asp:Menu> 

and here is the code behind:

  Protected Sub Page_Load(sender As Object, e As EventArgs) If Not Roles.IsUserInRole("Admin") Then Dim menuItems As MenuItemCollection = NavigationMenu.Items Dim adminItem As New MenuItem() For Each menuItem As MenuItem In menuItems If menuItem.Text = "Admin" Then adminItem = menuItem End If Next menuItems.Remove(adminItem) End If End Sub 
+4
source share
1 answer

Probably the easiest way is to use the SiteMapProvider configured with SecurityTrimmingEnabled = true.

A related MSDN article links to many How-To articles to help you get started.

+1
source

All Articles