Roles and Profiles
The first part of this extract reflects the setting applied in the site administration tool, denying all users access:
<deny users=”*” />
The second part of the extract applies access permissions to a specific page — the Admin.aspx page, in this case. The users who are allowed to view the Admin.aspx page include anyone who is a member of any of the Reporter, Administrator, Owner, or Manager roles:
<allow roles=”Reporter,Administrator,Owner,Manager” />
And so the pattern continues for the remainder of the configuration file, where access to each page is controlled by the configuration file, enabling access permissions to be set for each role against each page in the hierarchy.
Controlling Page Visibility
There may be some situations where you will want to allow a user to know of the existence of a page, even if the user is denied access to it. In this way, you can offer a link to users, but when they click that link, they are prompted to log in as a user with sufficient privileges before they can view the page. In order to achieve this, a roles attribute (see the shaded lines in the following code) can be added to the web.sitemap file, which ensures that a link will be visible to certain users to specified pages, even if the user is unable to view the content of the page itself. Here is an example:
<siteMapNode title=”Administration” url=”Admin/Admin.aspx” roles=”Reporter,Owner,Manager,Administrator”> <siteMapNode title=”Edit News” url=”Admin/EditNews.aspx”
description=”Edit club news” roles=”Reporter,Owner,Manager,Administrator” />
<siteMapNode title=”Schedule Match” url=”Admin/ScheduleMatch.aspx” description=”Schedule a Match”
roles=”Manager,Owner” />
Notice that the roles attribute on the Admin.aspx page states that Reporters, Owners, Managers, and Administrators should be aware of the existence of the Admin.aspx page. Contrast this with the ScheduleMatch.aspx page, and you’ll see that only Managers or Owners can see the link to be able to schedule matches.
If you log in to the site as ChrisH, you’re a member of the Reporter role, so even if all access to all Admin pages is revoked, you will still see links to the Admin.aspx and EditNews.aspx pages, but you won’t see a link to the ScheduleMatch.aspx page.
Because the Web.config file and the roles attributes in the web.sitemap work together to control both visibility and access, the best way to understand how they work together is to see them in an example.
For this Try It Out, make sure you are using the Chapter 11 version of the Wrox United code. The code supplied has been modified slightly for the purposes of this example. Also, make sure you close any open browser windows from the previous exercise, or your changes may not appear as expected.