Monday, November 12, 2012

Create asp.net MVC 4 supported membership database

In traditional asp.net, you can run aspnet_regsql to create the membership database.

In this one I will explain how to create mvc 4 membership database.

1. In web.config, add the default (localdb) connection

    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=dbname;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\dbname.mdf" />

2. Install the necessary packages reference in NuGet
Install-Package DotNetOpenAuth
Install-Package DotNetOpenAuth.aspnet
Install-Package Microsoft.AspNet.WebPages.OAuth
Install-Package WebGrease
Install-Package WebMatrix.Data
Install-Package WebMatrix.WebData
Add reference to system.web.helpers from nuget packages, make sure copy to local is true

3. Include the codes from an internet application sample
\Models\AccountModels.cs
\Filters\InitializeSimpleMembershipAttribute.cs
\Controller\AccountController.cs
\Views\Shared\_LoginPartial.cshtml
\Views\Account\* (update the namespace)

4. Also in web.config, remove the default membership provider and role provider section.

5. To use OAuth with Facebook, Google, Twitter etc,
Need to include the file \App_Start\AuthConfig.cs to uncomment the provider code with their appid and app seceret.
Also modify the global.asax.cs to add the line of the code,
AuthConfig.RegisterAuth();

No comments: