Search code examples
c#asp.net-mvc-4razorsimplemembership

How to introduce Simplemembership to an existing MVC 4 application?


I know the question looks very broad. Basically I have an existing MVC 4 application which I based around the tutorial found in the ebook Pro ASP.NET MVC 4, 4th Edition. I am working with an existing database, which for now only has one table. I want to introduce functionality to allow for user's to register and log-in. As I understand it, this means using Simplemembership (correct?). But I unclear how to start ? .... will Simplememebership create the required tables automatically ? If anyone has clear instructions / steps how to start, or a simple yet clear tutorial they know of, I very much appreciate it. Again please note - this is an ASP.NET MVC 4 Internet Application built using VS 2010 connected to SQL express database, using Razor views.


Solution

  • Start a new ASP.NET MVC 4 project in Visual Studio 2012 so you can see the default template

    Then copy Filters\InitializeSimpleMembershipAttribute.cs to your existing application

    Thereafter add the attribute [InitializeSimpleMembership] to the AccountController in your existing application

    Lastly you can copy the Login, Logoff and Register methods to the AccountController in your exising application

    You will also need some settings in your Web.config. Have a look in the Web.config file in the default template. I think these should be enough

    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20141017110935;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20141017110935.mdf" providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    and

    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    </entityFramework>