Microsoft has introduced its default database schema which is served as a role provider, membership provider and profile provider in .NET 2+.
In .NET 4, the database is created automatically, and is added to your App_Data folder and an appropriate connection string will be added to your web.config. But to be able to use this feature in previous versions of .NET, you have to create the database yourself and configure your web.config to point to that database.
For the web.config part, you have to add the following part to it:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
where
SERVER_NAME
and
DATABASE_NAME
should be replaced by your server/database name.
But how do you create the database with the default ASP.NET membership schema? The first solution is that you can use the
aspnet_regsql
command. A step by step tutorial can be found at
http://programming.top54u.com/post/How-to-Create-Aspnetdb.aspx[
^]
But as far as I can remember, this command tool was a bit tricky for me and didn't always work as expected especially if you have more than one website in your dev machine that uses Aspnetdb. So I searched for a better solution and here it is:
1- Create your database in SQL server.
2- Open up your ASP.NET website in which you'd like to use ASP.NET membership features.
3- Add the following using (or imports in case of VB) directive to your
default.aspc.cs (or
.vb)
using System.Web.Management;
4- In the load event of your website, add the following method call:
System.Web.Management.SqlServices.Install("YOUR_SERVER", "YOUR_DATABASE", SqlFeatures.All);
Just replace the
SERVER_NAME
and
DATABASE
with the appropriate values.
Also, if you're using SQL Express edition, there's an overload for that as well:
System.Web.Management.SqlServices.Install("YOUR_DATABASE", SqlFeatures.All, "YOUR_CONNECTION_STRING");
Now fire up your site and navigate to
default.aspx. The code gets executed and if you open your database in SSMS, you'll see that default scripts have been executed on your database to create the default database for ASP.NET 2.0 web applications. You can now delete the command from your page's load event.