ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

0
105
7 min read

Now that you understand the process behind forms authentication, we need to add it to our application. The process will be slightly different because we already have a database to use, but without the ASP.NET membership schema. We’ll add that to the database and then create some user accounts and membership roles to handle the security for our application. We’ll also secure some of our content and add a menu to our Master Page to navigate between the pages of our Content Management System.

Preparing an existing SQL database

As we have an existing database, we can’t create a new database for our membership and authentication system. Well, actually we could, but using a second database is problematic when we upload the application to a host because many web hosting companies allow only a single database under the hosting plan. Besides, we can easily add the membership schema the same way we did earlier in the article with our empty database, using aspnet_regsql.exe. Previously we used the wizard, this time we’ll use the command line. If you take a look at the database in SQL Server Management Studio Express now, before we execute the command to add the schemas, you should see the few tables that were already created, as shown below:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

The aspnet_regsql.exe tool

Using the command line, the executable is simple, as long as you know the command line arguments. The syntax and command arguments for aspnet_regsql.exe are available online at http://msdn.microsoft.com/en-us/library/x28wfk74.aspx. The following table shows the arguments we will use:

Argument

Description

What we use

-S

The server name

SQLEXPRESS

-U

The database username

sa

-P

The database password

SimpleCMS

-d

The database name

SimpleCMS_Database

-A

The schema functions to install

All functions

 

Our command line will look like this (all one line):

aspnet_regsql.exe -S .SQLEXPRESS -U sa -P SimpleCMS -d SimpleCMS_
Database -A all

To run the command line, go to Start | Run and enter cmd in the Run dialog box. Press Enter and you will be at a command prompt. Type cd C:WINDOWSMicrosoft.NETFrameworkv2.0.50727 and press Enter again, and you will be in the correct folder to find aspnet_regsql.exe. Note that you may need to change the path if your ASP.NET framework files are in a different location. Type the command line above and press Enter, and you should see that the command completed successfully, with a dialog similar to that below:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

Now that we have executed the aspnet_regsql.exe command line, if you look at the database tables in SQL Server Management Studio Express, you should see the added table for the users, membership, and roles we will use in our application.

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

User accounts

Earlier in the article, we created a single user account for accessing protected content. In a real-world environment, we would normally have many user accounts, way too many to add each account to each page we wanted to protect. Fortunately, the ASP.NET framework provides us with membership roles that we can place user accounts in, allowing us to define our access by role, not by user account. But first, we need some user accounts.

Let’s start by creating three accounts in our application  – User1, User2, and Administrator. Open the SimpleCMS web site in Visual Web Developer 2008 Express. Use the downloadable code provided for Chapter 4 of this book, it has the web.config file modified similar to what we did when we walked through the forms authentication demo earlier in the chapter. Open the Web Site Administration Tool by clicking on Website and then ASP.NET Configuration.

If you click on the Security tab, you will see that we have no users configured for this application.  As you did earlier in the article, click on Create User and create the three users with user names of User1, User2, and Administrator. Use Password! as the password for each, and provide a valid email address for each (they can have the same email for testing). Also, provide a question and answer such as Favorite Color? and Blue. You can use the same question and answer for all three accounts if you wish. Each user entry should look something like the following:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

If you return to the Security tab, you will notice that we have three user accounts, but no roles for those accounts. Let’s add them next.

Membership roles

ASP.NET membership roles provide the ability to group many individual accounts into a single role to provide access to a resource such as a page or application. Changing access for an individual user then becomes a simple task of assigning them to or removing them from the appropriate role. A single user account can belong to multiple roles to provide extremely granular access to the application resources if your security demands are extensive.

To add roles to our application, we first need to enable roles. On the Security tab of the Web Site Administration Tool, under Roles, you should see a link to enable roles. Enabling roles consists of simply adding the following line to the web.config file in the system.web section:

<roleManager enabled="true" />

Similar to the membership provider we created earlier, roles require a role provider. We need to add this provider to the role manager, so edit the web.config roleManager section to read:

<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider"
connectionStringName="SimpleCMS_DatabaseConnectionString"
applicationName="/"
type="System.Web.Security.SqlRoleProvider, System.Web,
Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>

This adds an AspNetSqlRoleProvider that uses our connection string to the SimpleCMS database. At this point we have no roles defined, so let’s create a few. Open the Web Site Administration Tool. If it’s already open, you may need to close and reopen it because we modified the web.config file to add the role provider. Now, open the Security tab. In the Roles section, click on Create or manage roles.

Let’s create an administration role first. We’ll need it to secure areas to just administrative access. Simply enter Administrator, click on Add Role, and you’ll see the new role in the list. Add roles for Author, Editor, and Registered User in the same manner. The roles list should look something like the following figure when you finish:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

Adding users to roles

Once we have users and roles created, we need to assign users to roles. To do this, use the Security tab of the Web Site Administration Tool, under the Users section, to manage users.  You’ll see a list of user accounts, in our case all three of them, along with the ability to edit the user, delete the user, and edit the user’s roles. Click on Edit roles next to the Administrator user and you’ll see a checkbox list of user roles this account can be added to. Any roles currently assigned to the user will be checked. As there are currently none, check the Administrator role, and the Administrator user will be immediately added to the Administrator role, as shown below:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

If you were to look at the database tables that hold the user accounts and roles, you would see something like this for the users:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

Similarly, the roles would look like this:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

You’ll note that both the users and the roles contain an ApplicationID that defines what application these users and roles belong to, and that each user or role is identified by a UserID or RoleID. These are automatically created by the ASP.NET membership framework and are globally unique identifiers (GUIDs), which ensure that the specific user or role is unique across all possible applications and uses of this specific database store.

You would also find in the database a table that identifies users in roles, looking something like this:

ASP.NET 3.5 CMS: Adding Security and Membership (Part 2)

You’ll notice that this is a joining table, used in a database when there is a many-to-many relationship. Many users can belong to a role and a user can belong to many roles, thus the use of this table. You’ll also notice that the database table uses the UserID and RoleID, making it very hard to simply look at this table directly to find what users are assigned to what roles.  Fortunately, with the ASP.NET framework, you’re isolated from having to work directly with the database, as well as relieved from having to create it and the code needed to access it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here