Security Through Authorization

In the preceding section you saw that regardless of what means of authentication is used, all users will appear to ASP.NET pages as an instance of IPrincipal, which in turn has an Identity property of type IIdentity. Using these standard interfaces, your code can function properly under any authentication scheme. The next section of this chapter deals with the concept of authorization, which is the process by which an authenticated user is permitted or denied access to specific resources. In other words, authentication deals with who a user is, and authorization deals with what the user can do.

Authorization with Roles

As you saw in the preceding section of this chapter, authentication is supported largely by the Membership API and Membership providers like the SQL Membership provider. The Provider model is used throughout ASP.NET to create standard interfaces in commonly used design patterns. Membership is something that virtually every ASP.NET website has to deal with in some form, so the Membership provider was used to standardize how that is done, creating a huge benefit for developers.

Authorization in ASP.NET applications is largely supported by the Role provider. A Role provider is a pluggable provider that gives programmers a standard API for determining users' role membership as well as manipulating the roles to which users belong. If you use the provider model, the code for your role-based application will be identical whether the user role membership is stored in SQL Server, Access, Active Directory, or some other proprietary data store.

Just as with the Membership provider, you need to tell your application which Role provider you're using. The first step is to define a connection string. If you followed along with the preceding example, you already have a connection string in your Web.config file. The next step is to define the <roleManager> element. An example of a <roleManager> element is shown in the following code:

<roleManager   defaultProvider="SqlProvider"   enabled="true" cacheRolesInCookie="true"   cookieName=".ASPROLES" cookieTimeout="30"   cookiePath="/" cookieRequireSSL="false"   cookieSlidingExpiration="true" cookieProtection="All">   <providers>     <add name="SqlProvider"         type="System.Web.Security.SqlRoleProvider"         connectionStringName="LocalSqlServer"         applicationName="RolesDemo"/>   </providers> </roleManager> 

Access to the majority of the functionality available through the Role management provider is available through the Roles class. Table 28.3 lists some of the properties of the Roles class and Table 28.4 lists some of its methods that you will be using in your own role-based security implementation.

Table 28.3. Roles Properties




The name of the application for which role data is stored.


Indicates whether the role information is cached in a cookie. If there is more information than a cookie can hold, only recent Roles are stored in the cookie and the rest are fetched as needed.


Gets or sets the name of the cookie used for role caching.


The path of the cookie that was set by CookieName.


Indicates how the role names are protected within the cookie.


Indicates whether the role name cache cookie requires SSL in order to be given to the server.


The sliding expiration period for the role name cache cookie.


The timeout period for the role name cache cookie.


Indicates whether the cookie for storing role name caches is persistent or session-based.


The domain of the role name cache cookie.


Indicates whether role management is enabled for the current application. The default is true.


Indicates the maximum number of roles that can be cached for a user.


Gets the Role provider for the current application.


Gets the collection of all Role providers for the current application.

Table 28.4. Roles Methods




Adds a list of users to a given role.


Adds a list of users to a list of roles.


Adds a user to a role.


Adds the user to a list of roles.


Creates a new role in the underlying role store.


Deletes the role name cache cookie.


Deletes a role from the data source. Existing users will no longer be a part of the deleted role.


Returns the list of users in a given role that match the supplied username wildcard.


Returns the list of all roles configured in the system.


Returns the list of all roles to which the user belongs.


Returns the list of all users belonging to the supplied role.


Indicates whether the user (current or supplied) is in the supplied role.


Removes the user from the indicated role.


Removes the user from the indicated roles.


Removes the specified list of users from the specified role.


Removes the list of users from the specified roles.


Indicates whether a role with the supplied name exists in the data store.

As you will see in the next section, working with Users and Roles when using the Membership and Role providers has already been wrapped into a few extremely handy server controls that ship with ASP.NET 2.0. To see how the Role system works programmatically, try walking through a quick sample.

The first thing you need to do is create a user. To create a new user, you can use the Membership.CreateUser method as shown in the following code:

string newPassword = Membership.GeneratePassword(8, 2); MembershipCreateStatus status; Membership.CreateUser("kevin", newPassword,   "", "What is the answer?", "42", true,   out status); Response.Write("Attempt to create user 'kevin' with password '" +    newPassword + "' was " + status.ToString() + "<BR>"); 

When you have a user, you can start playing around with the Role membership system. For example, the following code creates several new Roles and adds the current user to a few of them:

Roles.CreateRole("Administrators"); Roles.CreateRole("Validated Users"); Roles.CreateRole("Applicants"); Roles.AddUserToRole("kevin", "Administrators"); Roles.AddUserToRole("kevin", "Validated Users"); Response.Write("User 'kevin' belongs to the following Roles:<BR>"); foreach (string roleName in Roles.GetRolesForUser("kevin")) {   Response.Write(     string.Format("<b><i>{0}</b></i><br>", roleName)); } 

I can't stress enough how important the impact of the provider model is. The common tasks of building a Membership and Role systemwhich most of us have built over and over again for many different ASP.NET applicationshave been completely abstracted into a provider model. This allows you to create standardized code that works against a standard Membership and Role system, and you will know that your code will work on any other application that is using the Membership and Role providers.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: