Knowledge is power. We love to share it.

News related to Mono products, services and latest developments in our community.


Adding ASP.NET SimpleMembership to an existing MVC 4 application

05/07/2013Categories: ASP.NET

Even though there is an MVC 4 template for a new Internet project that contains membership functionality, some people like to start building their apps from an empty project. In this blog post I'll try to sum up what you'll need to do to get the simple membership going in your existing projects.

There is a great blog post by Jon Galloway that explains the new simple membership in detail.

We can start by creating the database which I named “SimpleMembershipTest”. After that, I created a new MVC 4 project using the “Empty” template.

The next step is to add references to WebMartix.Data and WebMatrix.WebData which is shown in Image 1.

After you have added those, expand "References" in your Solution Explorer and go to properties of both of them by right clicking on each. In the properties window set "Copy Local" to True as shown in Image 2.

In the Web.config you'll have to add the connection string. I was using SQLEXPRESS database so my ConnectionString looks like this:

 <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SimpleMembershipTest;Integrated Security=True" providerName="System.Data.SqlClient" />

Next you'll have to add the membership provider that points to WebMatrix.WebData.SimpleMembershipProvider:

<membership defaultProvider="SimpleMembershipProvider">
add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>

Now enable the role manager:

  <roleManager enabled="true" />

Finally, in Global.asax.cs - Application_Start() method we have to initialize the membership system by calling WebSecurity.InitializeDatabaseConnection

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true); 

After that we can create our user, add a role, add the user to the role and more:

WebSecurity.CreateUserAndAccount("Admin", "Admin");             
Roles.AddUserToRole("Admin", "Administrator");

After running the application, our membership tables are created which can be seen in Image 3.

And that's it! On your next application start, the table "UserProfile" will be created along with the rest of the SimpleMembership tables. From there you can build your own Registration and Login with the help of the functionality that System.Web.Security.Roles and WebMatrix.WebData.WebSecurity provide.

Rated 2.95, 40 vote(s). 
Thanks, great post. For information this also works on WebForms. I've been trying to find something that works with webforms and this post works perfectly.

By Nevo
Hi Dalibor, nice read. Anyway can you shortly explain to me why fresh MVC 4 template for a new Internet project that contains membership functionality does not work after publishing and fresh MVC 3 template does work? I mean I can not open Login or Register page in MVC4 while in MVC3 there is no problem with this pages. thx
Nice article!
By Jason
Great Article! Explained very simply.
Just wanted to say that <clear/> in the provider is unnecessary and can cause problems at times during the IntializeDatabaseConnection call saying that the default membership provider is unavailable.
Your recipe doesn't work. I'm giving up after four errors/omissions in Web.Config, followed by an exception on the init call.

Do better.
Before you say "Do better" please try to be less judgmental and provide us with the exception you get, maybe we need to fix something in article, e.g. if there is an issue in the steps or code. This way we can help other readers too.

Hey, thanks for putting this together. I hate all the extra that comes with not using an empty template. I've been havbing some trouble getting this up and running though. I Keep getting 2 errors. 1st is that from the global.asax file, it says my connection string can't be found, and the second is that the membership section in the web.config file is saying there is a missing section. Just fyi, I was trying to implement this with a LocalDB using MVC5. Any thoughts would be great. Thanks again!
By Gaz
Thank you, you saved a lot of frustration for me with this!
Thank you very much! It helped me a lot!!!
By Michael
Hi Dalibor,

I used your solution and everything was just fine. But now I'm getting:
An error occurred during the execution of the SQL file 'InstallCommon.sql'. The SQL error number is 5123 and the SqlException message is: CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\XXXX\APP_DATA\ASPNETDB_TMP.MDF'.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Creating the ASPNETDB_18ded8d2e5b341589f1a5ce3cd52f942 database...

I already have all tables created. It happens after some time of inactivity. The only way to get login page is to delete .ASPXAUTH cookie.

I hope you can help me to resolve this issue.

P.S. You can reply to my email.
1 2