FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Advanced User Authentication with .NET and Dreamweaver, Part 2

By: Heidi Bautista

Page 1 of 6

Set for printing

Next

Recap

In Part 1 of this series you learned how to create a Forms-based authentication scheme for a single type of user. In this part, you'll learn how to extend your scheme so that you can authenticate two different kinds of users by assigning them different roles.

Part 1: Forms Authentication for a single type of user

Part 2: Forms Authentication for multiple roles

Appendix A: Setup the Testing Server and Create the Database Connection

Code Listings

Part 1: Forms Authentication for a single type of user

Part 2: Forms Authentication for multiple roles

Adding a Second Login Role for the Manager

Up to now, your authentication scheme has allowed for just a single type of login: members. Logged in members are allowed to access restricted pages that reside in the "members" folder. Whenever you have members, however, you invariably need a super-user to manage all members' accounts. This is the manager account, and it's the only user with unrestricted access to the pages in the "manager" folder (typically, this folder contains pages that allow the manager to administer the members' accounts). The manager can access member-only pages, too, of course.

Forms authentication in ASP.NET supports multiple roles but requires additional programming:

  1. You can no longer use the default FormsAuthenticationTicket created by RedirectFromLoginPage because you need to use the userData attribute of the ticket to store your role information. Also, you don't want to have to use default.aspx as the default page for redirecting your visitors.
  2. You need to discern the user's role information when they request restricted pages. For the simpler authentication scheme discussed in the previous sections, you looked at the web.config and login.aspx files. To support multiple roles you must edit the global.asax file, too.

In the next section, you'll take a look at the changes needed in the web.config file.

Creating Multiple Roles in the web.config File

The site manager needs exclusive access to pages in the manager folder and to pages in the members folder; likewise, logged in members must have access to pages in the members folder. Table 1 shows the modifications required for the web.config file to support this more complex authentication scheme.

Because you have two separate folders with restricted-access pages, you need two separate the <location> tags to define the folders' paths.

Table 1. Modifications Required for web.config

Replace this code:
<!-- Require authorization for all -->
<!-- files in the "member" folder  -->

<location path="members">
  <system.web>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
</location>
With this code:
<!-- Require authorization for all -->
<!-- files in the "manager" folder -->
 
<location path="manager">
  <system.web> 
    <authorization>
      <allow roles="manager" />
      <deny users="*" />
    </authorization>
  </system.web>
</location>
 
<!-- Require authorization for -->
<!-- all files in the"member"  -->
<!-- subdirectory. Note the    -->
<!-- multiple values for the   -->
<!-- <allow roles> tag. This   -->
<!-- allows a person logged in -->
<!-- as a manager to see the   -->
<!-- member pages too.         -->
 
<location path="members">
  <system.web> 
    <authorization>
      <allow roles="member, manager" />
      <deny users="*" />
    </authorization>
   </system.web>
</location>

 

Look at the code snippet on the right side of Table 1. The path in the first <location> tag indicates that the following authorization rules apply to pages within the manager directory only. Upon login, the <authorization> tag allows any user in a manager role to access files and denies all other users (shown by the * in the code), even if the page has authenticated the user with any role other than the manager role.

The second location path specifies the members directory. The <authorization> tag allows access by any user in the member and/or manager roles and denies all other users (shown again by the *).

The code that you replace (left side of Table 1) applies to the unique case of a single type of login with no specific role assigned to it. The question mark in the <deny users> tag means that all unauthenticated users are denied access.

Now let's look at how roles are assigned.

Page 1 of 6 1 2 3 4 5 6 Next


download
Download Support Files


Keywords
Forms authentication, FormsAuthenticationTicket, ASP.NET, Dreamweaver, DataSet, SQL Server, C#, roles