6. Security Trimming
The ASP.NET navigation support works with the authentication and authorization mechanisms to support security trimming. Security trimming
means showing only part of the menu based on the role of the current
user. Of course, this means that the Web site must somehow authenticate
the user.
To make security trimming work, turn the securityTrimmingEnabled attribute on in web.config. The list of roles for which the navigation option is available is a property for each SiteMapNode.
Finally, the ASP.NET navigation architecture supports URL mapping. URL mapping is mapping a virtual (or nonexistent) URL to an existing ASPX file in the web.config file using the urlMappings
element. Setting up URL mappings causes ASP.NET to read the requested
URL and uses the handler for the mapped URL. This is done in HttpApplication using HttpContext.RewritePath.
For example, imagine your Web site includes a single products page
containing both CDs and DVDs. However, your UI model requires you to
build a menu structure that separates the CD products and the DVD
products into two options that appear separately on the menu. URL
mapping provides a way of handling this situation.
Here's an exercise showing how to use URL mapping to represent a
single page as two separate menu items. In this case, the page's content
is distinguished by a URL parameter.
Implementing URL mapping
-
Update the Products page so that it shows different content when the
ID parameter is 1 or 2. This example divides the products into CDs and
DVDs. The page displays different content based on the value of the ID
parameter (whether it's 1 or 2 or something else). Place a Label control on the Products page and assign its ID property the value LabelProductType. Then, drop a ListBox on the page and assign its ID the value ListBoxProducts. The code-beside file then implements the URL mapping functionality in the Page_Load handler, as shown here:
public partial class Products : System.Web.UI.Page
{
protected void AddCDsToListBox()
{
this.ListBoxProducts.Items.Add("CD- Snakes and Arrows");
this.ListBoxProducts.Items.Add("CD- A Farewell To Kings");
this.ListBoxProducts.Items.Add("CD- Moving Pictures");
this.ListBoxProducts.Items.Add("CD- Hemispheres");
this.ListBoxProducts.Items.Add("CD- Permanent Waves");
this.ListBoxProducts.Items.Add("CD- Counterparts");
this.ListBoxProducts.Items.Add("CD- Roll the Bones");
this.ListBoxProducts.Items.Add("CD- Fly By Night");
this.ListBoxProducts.Items.Add("CD- 2112");
}
protected void AddDVDsToListBox()
{
this.ListBoxProducts.Items.Add("DVD- A Show Of Hands");
this.ListBoxProducts.Items.Add("DVD- Exit Stage Left");
this.ListBoxProducts.Items.Add("DVD- Rush In Rio");
this.ListBoxProducts.Items.Add("DVD- R30");
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request.Params["ID"] == "1")
{
this.LabelProductType.Text = "CDs";
AddCDsToListBox();
}
else if (this.Request.Params["ID"] == "2")
{
this.LabelProductType.Text = "DVDs";
AddDVDsToListBox();
}
else
{
this.LabelProductType.Text = "All CDs and DVDs";
AddCDsToListBox();
AddDVDsToListBox();
}
}
}
-
Update the web.sitemap file to include the new menu items mapped to virtual files (for example, CDs.aspx and DVDs.aspx). Add this to the Web.site file:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home"
description="This is the home page"
ImageURL="~/homeimage.jpg">
<siteMapNode url="~/Products.aspx" title="Products"
description="This is the products page"
ImageURL="~/productsimage.jpg">
<siteMapNode url="~/CDs.aspx" title="CDs"
description="This is the CDs page"
ImageURL="~/productsimage.jpg"/>
<siteMapNode url="~/DVDs.aspx" title="DVDs"
description="This is the DVDs page"
ImageURL="~/productsimage.jpg"/>
</siteMapNode>
<siteMapNode url="~/Support.aspx" title="Support"
description="This is the support page"
ImageURL="~/supportimage.jpg"/>
<siteMapNode url="~/Contact.aspx" title="Contacts"
description="This is the contacts page"
ImageURL="~/contactimage.jpg">
<siteMapNode url="~/ContactAddress/ContactAddress.aspx"
title="Contact using physical address"
description="This is the first contact page"
ImageURL="~/contactPhysicalAddressimage.jpg"/>
<siteMapNode url="~/ContactEmailPhone/ContactEmailPhone.aspx"
title="Contact by email or phone"
description="This is the second contact page"
ImageURL="~/contactPhoneimage.jpg" />
</siteMapNode>
</siteMapNode>
</siteMap>
-
Add this to the web.config file:
<configuration>
<system.web>
<urlMappings enabled="true">
<add url="~/CDs.aspx" mappedUrl="~/Products.aspx?ID=1"/>
<add url="~/DVDs.aspx" mappedUrl="~/Products.aspx?ID=2"/>
</urlMappings>
</system.web>
</configuration>
-
Run the page. Notice that changes occurred and two new items now
appear on the Products menu. The site map points these two items to the
CDs.aspx file and the DVDs.aspx file. Although the application does not include files with
these names, users still see a page that works when they redirect using
one of these menu items. The web.config file remaps the request back to
the Products.aspx page, passing a URL parameter with a specific value.
When the Products.aspx page is loaded and the ID parameter is 1 or 2,
the page loads the list box with CD titles or DVD titles.
The following graphic shows the CDs Products page being selected from the site map data:
The next graphic shows the DVDs Products page being selected from the site map data:
This graphic shows the main Products page being selected from the site map data:
URL mapping is useful in all kinds of situations when you need to represent pages in a navigation control, even when there might not be a physical page to support it.
Microsoft Internet Information Services (IIS) 7.0 now includes a URL
Rewrite Module that supports the more contemporary technique of URL
rewriting. URL rewriting is a much more dynamic technique for redirecting requests than is the simple configuration file urlMappings
technique used in the last example. For example, you can use URL
rewriting to redirect based on various run-time criteria, such as server
variables or HTTP headers. You can also set up redirects based on URL
rewriting rules.