Figure 13.5. The ASPNETDB database files
automatically, without any configuration. Should you need to access this database
through your own code (for example, to customize the data structures), you can
do so using the special connection string LocalSqlServer.
There are two things you need to be aware of when using the ASPNETDB database:
Although User Instance databases were designed to be easy to move between
systems, you cant always easily upload them to a hosting server.
It will often result in your application having to work with two databases.
In our case, the Dorknozzle site would need to use both the ASPNETDB database
and our old friend, the Dorknozzle database. Whether this is a wise choice
or not depends on the specifics of your project, and whether your sites other
data structures need to relate to the membership data of your users.
Fortunately, you have the option to create the necessary data structures within
your existing database.
Using your Database to Store Membership Data
In many cases, its more beneficial to store the membership data structures in
your own database than in the default ASPNETDB database. Indeed, for the purposes
of our application, it would be preferable to keep that data inside the existing
Dorknozzle database. This way, when we launch the project, well need to
547
Using your Database to Store Membership Data
transfer only one database to the production machine, rather than having to
migrate two separate databases.
In order to use your database to store membership data, you need to complete
two tasks:
Create the necessary data structures in Dorknozzle.
Edit Web.config to specify the new location of these structures, overriding
the default configuration that uses the ASPNETDB database.
You can use a tool that ships with ASP.NET 2.0, aspnet_regsql.exe, to custom-
ize the data store and add the necessary structures to your own database. This
tool can be executed at the Windows command prompt, where you can include
various parameters to instantly configure it for your database; alternatively, it
can be run in Wizard mode, allowing you to set those options one at a time. To
execute the tool, first open a SDK Command Prompt window from the Start menu
(Start > All Programs > Microsoft .NET Framework SDK 2.0 > SDK Command
Prompt). At the command prompt, type aspnet_regsql.exe and hit Enter.
The wizard should open with a Welcome screen, where youll just need to click
Next. In the next window, which is shown in Figure 13.6, you can choose between
adding the data structures to an existing database (or to a new database that can
be created for you), or removing the data structures.
Leave the first option selected and hit Nextyoull see the dialog shown in Fig-
ure 13.7. Here, youll need to tell the wizard which server and database you want
to connect to in order to create those structures. The Server should be LOCAL\Sql-
Express (where LOCAL is the network name of your current machine), and the
Database should be Dorknozzle.
Using Remote Servers
You can enter any local or remote server into the Server field. You might
use this tool to configure a remote server, for example, when choosing to
move the application from your development machine to a remote production
machine. In that case, youd need to select SQL Server Authentication and
supply the username and password provided by the servers administrator.
548
Chapter 13: Security and User Authentication
Figure 13.6. The SQL Server Setup Wizard
Figure 13.7. Selecting the Server and Database
549
Using your Database to Store Membership Data
Click Next, and youll be asked to confirm the data youve entered. Hit Next
again, and expect a delay while the tool finishes setting up the database for you.
When its done, click Finish to close the wizard; then, when aspnet_regsql.exe
finishes executing and a new prompt is displayed, you can close the SDK Com-
mand Prompt window.
More on aspnet_regsql.exe
You can customize or automate this process by supplying parameters to the
aspnet_regsql.exe program at the command prompt. For example, instead
of running the wizard using the steps above, you could have executed this
command at the SDK Command Prompt:
aspnet_regsql -S LOCAL\SqlExpress -E -A all
-d Dorknozzle
To have the tool connect using SQL Server Authentication instead of the
integrated Windows Authentication, youd use a slightly different command:
aspnet_regsql -S LOCAL\SqlExpress -U username
-P password A all -d Dorknozzle
Keep in mind that you can also use this tool to configure a remote database,
in which case youd need to mention the remote server address and database
instance name instead of the local machine name and local SQL Server in-
stance name.
If you executed the commands shown above, the output would look like that
shown in Figure 13.8.
Figure 13.8. Using aspnet_regsql.exe at the command
prompt
550
Chapter 13: Security and User Authentication

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.