Referencing the ASPNETDB Database

Published 13 January 06 09:09 AM | Scott Mitchell

One of ASP.NET 2.0's coolest new features is the Membership system, which provides a standardized means for implementing user accounts on a system along with an API for interacting with the user accounts in the system. The Membership system using the provider model, meaning that you can customize the inner working of Membership if needed in order to use your custom user account data store. You can learn more about Membership and its cousin systems, Roles and Profile, in the multi-part article series Examining ASP.NET 2.0's Membership, Roles, and Profile (subscribe).

As discussed in the article series, the default SQL-server based implementation of ASP.NET 2.0's Membership system uses a predefined SQL Server database format that includes tables to support user accounts, membership details, roles, and profiles. When you configure your website to use forms-based authentication through the ASP.NET Website Administration Tool, it automatically creates a SQL Server 2005 database that has this schema and places it in your App_Data folder.

You can customize the default Membership settings - such as allowing passwords with just five characters, or requiring two non-alphanumeric characters - by adding a new Membership provider in the Web.config that uses the same type (SqlMembershipProvider), but uses custom values for properties like minRequiredPasswordLength, minRequiredNonalphanumericCharacters, and so on. When you do this, however, you must also provide a value for the connectionStringName property, which indicates the name of the connection string to use to connect to the SQL database that implements the default Membership schema.

In Part 1 of the article series I show how to specify a custom connection string name, but recently was asked how to have it just use the default ASPNETDB database. The answer: use the connection string name LocalSqlServer. That is, you'd add something like the following to your Web.config file:

<membership defaultProvider="CustomizedProvider">
add name="CustomizedProvider"
minRequiredNonalphanumericCharacters="0" />

That's it! In theory, you could also use this connection string to directly reference the ASPNETDB database through code, such as in a SqlDataSource control. However, ideally you will only interact with this database through the API (i.e., the Membership class).

You can see where this connection string name is defined if you look in the machine.config file in the WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG directory. This file contains default settings for all websites on the web server. In there you'll find:

add name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"

When this connection string is used, ASP.NET automatically resolves the substring |DataDirectory| to the executing application's physical path's App_Data folder. (You'll have to look into the bowels of the .NET Framework using Reflector to see where that's done.)

Filed under:


No Comments


My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.