Adding Membership and Roles to your MVC or ASP.NET Web Site - Application Services
There are several ways to add Application Services(membership, roles, etc.) to your web project. Depending on your deployment needs the different ways of adding these services are not all equal.


SET @dbname = N'aspnetdb'
IF (NOT EXISTS (SELECT name
FROM master.dbo.sysdatabases
WHERE name = @dbname))
BEGIN
PRINT 'Creating the ' + @dbname + ' database...'
DECLARE @cmd nvarchar(500)
SET @cmd = 'CREATE DATABASE [' + @dbname + '] ' + @dboptions
EXEC(@cmd)
END
GO
USE [aspnetdb]
SET @dbname = N'YourDbName' USE [YourDbName]
SET @dbname = N'aspnetdb'
IF (NOT EXISTS (SELECT name
FROM master.dbo.sysdatabases
WHERE ('[' + name + ']' = @dbname OR name = @dbname)))
BEGIN
RAISERROR('The database ''%s'' cannot be found. Please run InstallCommon.sql first.', 18, 1, @dbname)
END
GO
USE [aspnetdb]
SET @dbname = N'YourDbName' USE [YourDbName]
SET @dbname = N'aspnetdb'
IF (NOT EXISTS (SELECT name
FROM master.dbo.sysdatabases
WHERE ('[' + name + ']' = @dbname OR name = @dbname)))
BEGIN
RAISERROR('The database ''%s'' cannot be found. Please run InstallCommon.sql first.', 18, 1, @dbname)
END
GO
USE [aspnetdb]
SET @dbname = N'YourDbName' USE [YourDbName]
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Note the Connection string name above "ApplicationServices" you will also find it in each of the providers below, the connection string name can be anything you want, but the providers need to use the same name. Since we are using our own database rather than the aspnetdb database you will need to modify the connection string to point to your database.
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/"
/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ApplicationServices"
applicationName="/"
/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
When the connection string is modified to point to your database, the connection string name changed to what you want to call it, and that change rippled through the membership and role providers all you need to do now is compile your MVC project in order to use the ASP.NET Configuration menu item on the project menu (Web Site Administration Tool).
Now you have one database for your site, with the Application Services added to that database and the ability to use the Web Site Administration Tool (WSAT) from Visual Studio to set up and mange your users and roles as you develop your application. When it comes to inital deployment you can also resue the SQL scripts you modified to create the Application Service pieces in your hosted database.
