Scott on Writing

Musings on technical writing...

Referencing the ASPNETDB Database

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">
  <providers>
    <
add name="CustomizedProvider"
         type="System.Web.Security.SqlMembershipProvider"
         connectionStringName="LocalSqlServer"
         minRequiredPasswordLength="5"
         minRequiredNonalphanumericCharacters="0" />
  </
providers>
</
membership>

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:

<connectionStrings>
   <
add name="LocalSqlServer"
       
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
        
providerName="System.Data.SqlClient"/>
</
connectionStrings>

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.)

posted on Friday, January 13, 2006 9:09 AM

Feedback

# re: Referencing the ASPNETDB Database 1/17/2006 10:43 AM Atif Aziz

Thought you might like to know that the value of DataDirectory comes from interrogating AppDomain.CurrentDomain.GetData("DataDirectory"). Obviously, it’s not documented (at least not yet) so it shouldn’t be counted on by applications, but it’s still nonetheless good to know for rainy days.

There's some addition information in a post [1] over at the Smart Client Data blog [2].

[1] http://shrinkster.com/b3c
[2] http://blogs.msdn.com/smartclientdata/

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles