Using SimpleMembership in MVC3
I have received a couple of questions over the past few weeks regarding how to properly get SimpleMembership to work with an MVC3 application. While it is pretty straightforward, the purpose of this post is to serve as a step-by-step “How To” setup SimpleMembership with an MVC 3 application from start to finish.
The following 4 simple steps will get you up and running with SimpleMembership in your MVC3 application in a matter of minutes. We’ll go into each step in more detail below.
- Create new Mvc3 Internet Application or use an existing one.
- Create your SqlServer, SqlExpress or SqlCompact4 database (we’ll be using SqlCompact4 in this example)
- Update Web.config with connection string to point to your database from step 2
- Install & Configure SimpleMembership.Sample package
Creating your MVC 3 Application
- Select File > New Project and Select ASP.NET MVC 3 Web Application.
- Pick your desired folder location and click Ok.
- Next, make sure Internet Application is selected and Razor is the View engine.

- In this case, we chosen to create a unit test project automatically for testing purposes.
- Choose OK to create your project.
Creating your database (SqlCompact4)
- Before we do anything else, we are going to create a SqlCompact4 database rather than using SQLExpress which is the default for new projects.
- To do so, right-click the App_Data directory and select Add > New Item.
- In the next dialog, select SQL Server Compact 4.0 Local Database, use “Database.sdf” as the filename and click Add.
Updating Web.config connection string
Now that our database is created, we need to update our connection string in Web.config to point to our SqlCompact4 database we just created.
- Open Web.config and locate the connection string named “Application Services”.

- Next, change the “Application Services” connectionString and providerName to the following:
<add name=“ApplicationServices“ connectionString=“data source=|DataDirectory|\Database.sdf“ providerName=“System.Data.SqlServerCe.4.0“ />
Installing & Configuring SimpleMembership
Now, that we have our application ready, SqlCompact database created and ready to go, now we can install SimpleMembership. For the sake of simplicity, I’m going to install the SimpleMembership.Mvc3.Sample package which include the sample account controller pre-wired for use with SimpleMembership.
- In Visual Studio, navigate to Tools > Library Package Manager > Manage NuGet Packages.
- Select Online from the left-hand pane and type in SimpleMembership in the search box at the top right.
- Select the SimpleMembership.Mvc3.Sample package and click Install.

- Next, delete the existing Controllers/AccountController.cs file that Visual Studio created for you.
- Rename Controllers/SimpleMembershipAccountController.cs to AccountController.cs
- Open App_Start/SimpleMembershipMvc3.cs and update it to point to the “ApplicationServices” connection string or your appropriate connection string.
That’s It!
You’re application is now ready to use SimpleMembership and you have a fully-functional AccountController.cs pre-wired to use the SimpleMembership API.
Enjoy!
Please post a comment if you have questions or run into issues.
The code is available here: SimpleMembership.Mvc3.Example.zip

about 1 year ago
Nice post,
by the way, I have installed .mdf and found it not work with connection string, is it support provider System.sql.client? or it just support for SqlServer and Sql Compact Edition?
about 1 year ago
@Fazar,
It should support SqlExpress fine. Just make sure you have the right connection string and a provider of “System.Data.SqlClient”.
I have tested it with SqlCompact, SqlExpress and SqlServer.
about 1 year ago
Could this be made possible to work with System.Web.Providers? http://www.hanselman.com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx
Trying to figure what if any would have to change.
Thanks!
about 1 year ago
Will this work with a MySQL database?
about 10 months ago
@Sean,
Sorry for the delay in my response. Short answer is “no.” The SimpleMembership provider provided by Microsoft was built with SQL Server in mind. However, conceivably, you could sub-class SimpleMembershipProvider or ExtendedMembershipProvider and create a version that is MySQL-compliant.
about 3 months ago
Hi,
How to use IWebSecurityService with migrations? Because I allways get error:
You must call the “WebSecurity.InitializeDatabaseConnection” method before you call any other method of the “WebSecurity” class. This call should be placed in an _AppStart.cshtml file in the root of your site.
Migrations is place on other project in the same solution. I didn’t this error before I install SimpleMembershup.Mvc3
about 3 months ago
@Romi,
You do have to call WebSecurity.InitializeDatabaseConnection somehow and if you’re using the IWebSecurityService interface, then you should call WebSecurityService.InitializeDatabaseConnection.
When do you get that error? Is it when trying to run a migration?
Let me know.
Adam