Can anyone help me with displaying a menu in ASP.Net?
I’ve got my Site.master page with a navigation menu
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
<asp:LoginView>
<LoggedInTemplate>
<asp:MenuItem NavigateUrl="~/Securedpage.aspx" Text="Securedpage"/>
</LoggedInTemplate>
</asp:LoginView>
</Items>
</asp:Menu>
I have to show the first menu link to everyone and the second only to loggedIn users. However the <asp:LoginView> seems to be unrecognised within the menu block. Is there any way out?
You can bind the menu items to a site map and use the roles attribute. You will need to enable Security Trimming in your Web.Config to do this. This is the simplest way.
Site Navigation Overview: http://msdn.microsoft.com/en-us/library/e468hxky.aspx
Security Trimming Info: http://msdn.microsoft.com/en-us/library/ms178428.aspx
SiteMap Binding Info: http://www.w3schools.com/aspnet/aspnet_navigation.asp
Good Tutorial/Overview here: http://weblogs.asp.net/jgalloway/archive/2008/01/26/asp-net-menu-and-sitemap-security-trimming-plus-a-trick-for-when-your-menu-and-security-don-t-match-up.aspx
Another option that works, but is less ideal is to use the loginview control which can display controls based on role. This might be the quickest (but least flexible/performant) option. You can find a guide here: http://weblogs.asp.net/sukumarraju/archive/2010/07/28/role-based-authorization-using-loginview-control.aspx