Listing 7 shows the master page where both the menu control
and the code that loads the site map XML file are located. When the master page
loads a check is made to see if a user is logged in. If they are, then the user
name is displayed at the bottom of the page and the site map XML file gets
loaded into the XML data source for the menu control using the user's name as a
filter for the group node's user attribute. If there is no user logged in then
the user name label is emptied and the site map is loaded using the word "none"
as a filter. It is important to make sure that you include a link to the login
page in one of the groups in the site map that allows anonymous access. This
way, the login link will always be displayed in the menu. After the user is
authenticated they will see the menus that they have access to.
Place the MasterPage.master file (Listing 7) and the home
page (Listing 8) in the applications root directory. Place the Default.aspx
from Listing 9 in the Restricted sub directory.
Listing 7 - This the master page for the
application (MasterPage.master)
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string u = HttpContext.Current.User.Identity.Name.ToLower();
if (u == String.Empty)
{
u = "none";
LabelUser.Text = "";
}
else
{
LabelUser.Text = "Current User: " + HttpContext.Current.User.Identity.Name;
}
XmlDataSource1.XPath = "site/group[contains(@users,'" + u + "')]";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="XmlDataSource1"
DataFile="~/App_Data/SiteMap.xml" runat="server"/>
<asp:Menu ID="Menu1" DataSourceId="XmlDataSource1" runat="server"
SkinID="BlueMenu" Orientation="Horizontal" StaticDisplayLevels="1">
<Databindings>
<asp:MenuItemBinding DataMember="group" TextField="title"
NavigateUrlField="url" />
<asp:MenuItemBinding DataMember="page" TextField="text"
NavigateUrlField="url" />
</Databindings>
</asp:Menu>
<br/>
</div>
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
<div>
<br />
<asp:Label ID="LabelUser" runat="server" ></asp:Label>
</div>
</form>
</body>
</html>
Listing 8 - Shows the default web form for the
sample web site using the master page
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Menu"
Theme="Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<p>This is the XML menu example default.aspx page.</p>
</asp:Content>
Listing 9 - Shows the default web form for the Restricted
sub directory
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Menu"
Theme="Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<p>This is the restricted directory default.aspx page.</p>
</asp:Content>