
How ASP.NET Role Management Works
To work with role management, you first enable it and optionally configure access rules that can take advantage of roles. You can then use role management functions at run time to work with the roles.
Role Management Configuration
To use ASP.NET role management, you enable it in an application's Web.config file by using a setting such as the following:
|
<roleManager
enabled="true"
cacheRolesInCookie="true" >
</roleManager>
|
A typical use for roles is to establish rules that allow or deny access to pages or folders. You can set up such access rules in the authorization section of the Web.config file. The following example shows how to allow users in the role of members to view pages in the folder named MemberPages and denies access to anyone else:
|
<configuration>
<location path="MemberPages">
<system.web>
<authorization>
<allow roles="members" />
<deny users="*" />
</authorization>
</system.web>
</location>
<!-- other configuration settings here -->
<configuration>
|
For more information about how to set up access rules, see ASP.NET Authorization.
You must also create roles such as manager or member and then assign user IDs to the roles. If your application uses Windows authentication, you use the Windows Computer Management tool to create users and groups.
If you are using forms authentication, you can set up users and roles with the ASP.NET Web Site Administration Tool. If you prefer, you can perform this task programmatically by calling various role-manager methods. The following example shows how to create the role members:
|
Roles.CreateRole("members")
|
|
Roles.CreateRole("members");
|
The following example shows how to add the user JoeWorden individually to the role manager, and how you can add the users JillShrader and ShaiBassli to the role members at one time:
|
Roles.AddUsersToRole("JoeWorden", "manager")
Dim userGroup(2) As String
userGroup(0) = "JillShrader"
userGroup(1) = "ShaiBassli"
Roles.AddUsersToRole(userGroup, "members")
|
|
Roles.AddUsersToRole("JoeWorden", "manager");
string[] userGroup = new string[2];
userGroup[0] = "JillShrader";
userGroup[1] = "ShaiBassli";
Roles.AddUsersToRole(userGroup, "members");
|
Note: |
|---|
The role management features are not available through the ASP.NET roles service. The roles service can return information only about a particular user.
|
Working with Roles at Run Time
At run time, when users visit your site, they establish an identity, either as a Windows account name or by logging into your application. (In an Internet site, if users visit your site without logging in (anonymously), they will have no user identity and therefore will not be in any role.) Information about the logged-in user is available to your application from the User property. When roles are enabled, ASP.NET looks up the roles for the current user and adds them to the User object so that you can check them. The following example shows how to determine whether the current user is in the role of member. If the user is in the role, the code displays a button for members:
|
If User.IsInRole("members") Then
buttonMembersArea.Visible = True
End If
|
|
if (User.IsInRole("members"))
{
buttonMembersArea.Visible = True;
}
|
ASP.NET also creates an instance of the RolePrincipal class and adds it to the current request context. This enables you to perform role management tasks programmatically, such as determining what users are in a specific role. . The following example shows how to obtain a list of the roles for the current logged-in user.
|
Dim userRoles() as String = CType(User, RolePrincipal).GetRoles()
|
|
string[] userRoles = ((RolePrincipal)User).GetRoles();
|
If you are using the LoginView control in your application, the control will check the user's roles and can dynamically create a user interface based on the user's roles.
Caching Role Information
If a user's browser allows cookies, ASP.NET can optionally store role information in an encrypted cookie on the user's computer. On each page request, ASP.NET reads the cookie and populates the role information for that user from the cookie. This strategy minimizes the need to read role information from the database. If the user's browser does not support cookies or if cookies are disabled, role information is instead cached only for the duration of each page request.