Reputation: 2075
I am new to this site and i have researched online so many times in the past and could not get this issue resolved. I basically want to hide a menu item if user is not authorized and i am using a windows authentication method. My problem with this code is that the Menu Item called "Admin" is hidden even though i am in the Admin role. I basically have a file called Admin.aspx under a folder called Admin. In the Admin folder, i have a web config and this is how i configured it:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="~/Admin/Authors.aspx"></location>
<system.web>
<authorization>
<deny users="?" />
<allow users="Perf_Mon" />
<allow roles="Perf_Mon" />
</authorization>
</system.web>
<system.webServer>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Deny" users="?" />
<add accessType="Allow" roles="Admin" />
</authorization>
</security>
</system.webServer>
</configuration>
I am also using the code behind in the Master page file since i have all my menu in there and the code is here:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'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
So i am wondering why the Admin Menu is hidden from me even though i am in the Admin role. I am not sure what i am doing wrong. thanks Here is the ASP file for the Master Page
<body>
<form id="Form1" runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
Home Page
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ <a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a>
]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold">
<asp:LoginName ID="HeadLoginName" runat="server" />
</span>! [
<asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out"
LogoutPageUrl="~/" />
]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<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/MyTest1.aspx" Text="Summary" />
<asp:MenuItem NavigateUrl="~/Users/MyTest2.aspx" Text="Details" />
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
it is referencing the Master page in this line how can we change it to reference the site map?
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeFile="Authors.aspx.cs" Inherits="Admin_Authors1" %>
Upvotes: 5
Views: 6770
Reputation: 1825
.Net Web does not provide properties per Menu Item to specify visibility. So it wont be possible to set specific menu items to 'appear' visible per user. But the caveat of this is that you can however just NOT create the menu item in the first place.
Eg: Do not add your menu item via the Visual Studio Designer view. Add the items that are static regardless of authentication level.
in the on Page Load Event - perform the check for authentication. If the user is authenticated, create the Menu Items you want to appear when authenticated, via the object oriented approach - utilizing their constructors and setting any needed properties. I do this in my Site.Master page: because I want the menu to behave this way for all pages.
But you can however do it per individual page if you need to.
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
MenuItem m = new MenuItem("Upload");
m.NavigateUrl = "~/Uploader/Upload.aspx";
NavigationMenu.Items.Add(m);
}
}
Upvotes: 1
Reputation: 160
First place this code in your web.config within your <system.web>
tags
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" siteMapFile="/NavMenu.sitemap" />
</providers>
</siteMap>
now add a new sitemap item to your project and name it NavMenu.sitemap, then populate with the following code:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="#" title="Menu" description="">
<siteMapNode url="~/Admin/Author.aspx" title="Admin" roles="Admin"/>
<siteMapNode url="http://www.google.co.uk" title="Google" roles="*"/>
<siteMapNode url="http://www.hotmail.co.uk" title="Hotmail" roles="*"/>
</siteMapNode>
</siteMap>
now in your site.master (or whereever you have placed your Menu) replace you menu with these two lines:
<asp:Menu ID="NavigationMenu" runat="server" DataSourceID="NavMenuDS"></asp:Menu>
<asp:SiteMapDataSource ID="NavMenuDS" runat="server" ShowStartingNode="False" SiteMapProvider="XmlSiteMapProvider"/>
Now I'll explain (or try) what each of these are: The first block of code is declaring the sitemap provider without this you can't read the sitemap file, NavMenuDS then uses this provider information to populate the NavigationMenu.
in the sitemap file you'll notice at the end of the admin line the role="Admin"
is specified, well that's your line, the rest of the items need to have role="*"
.
Any Q's fier them over....
Upvotes: 2