novian kristianto
novian kristianto

Reputation: 761

how to make simple membership provider from empty web application template in asp.net mvc 4?

How do I configure a Simple Membership provider starting from an empty ASP.NET MVC 4 template?

I've searched a lot on Google, Bing and many others, but I didn't get any positive response about the membership provider.

Can some one tell me the basics of the simple membership provider?

Upvotes: 5

Views: 2058

Answers (2)

Anas
Anas

Reputation: 5727

Here is a short article that describe step by step how to add ASP.NET SimpleMembership to an existing MVC 4 application :

http://www.mono-software.com/blog/post/Mono/226/Adding-ASP-NET-SimpleMembership-to-an-existing-MVC-4-application/

Upvotes: 2

Jan Kratochvil
Jan Kratochvil

Reputation: 2327

I've just gone through the process and the steps are as follows. I assume you will be using Entity Framework for data access and have it already set up:

  • Reference libraries WebMatrix.Data and WebMatrix.WebData. You'll find them under Assemblies/Extensions in the Add Reference dialog
  • Add the following sections into Web.config:
<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=LicenceAudit.mdf;Integrated Security=SSPI;attachDBFilename=|DataDirectory|\LicenceAudit.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
<system.web>
<membership defaultProvider="simpleMembershipProvider">
      <providers>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
  <providers>
    <clear/>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
  </providers>
</roleManager>
</system.web>
  • Add WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UsersTableName", "UserId", "UserName", true) to Application_Start() in Global.asax.cs
  • Make sure your database file exists and contains the appropriate table. The UserId property should be of type int.
  • Test everything out by executing WebSecurity.CreateUserAndAccount("testUser", "myStrongPassword"). If it passes, you are in the clear.

Upvotes: 3

Related Questions