Search code examples
asp.netasp.net-mvc-4forms-authenticationsqlmembershipprovider

Cant configure MVC 4 SqlMembershipProvider


I am setting up my MVC 4 website to use SqlMembershipProvider with data store as SQL Server Express 11.0.21xx

I have installed Universal Providers via NuGet
PM > Install-Package Microsoft.AspNet.Providers

When I run the app and go to localhost/Accounts/Register and submit the form, I get this error
To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

at this line
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

Accounts controller has attribute [InitializeSimpleMembership] set. But the tables are not created due to aforementioned error.

web.config section updated by NuGet
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider"
type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>

ConnectionString
<add name="DefaultConnection" connectionString="server=servername\instance;Database=imdb;User Id=sa; Password=passbird;" providerName="System.Data.SqlClient" />

Where's the problem? Should I use universal providers at all? I dont believe I should run aspnet_regsql cos of the new Account controller.


Solution

  • All I had to do was change DefaultMembershipProvider to SimpleMembershipProvider in web.config. type attribute is important to get it right.

    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" 
             type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" 
             connectionStringName="DefaultConnection" 
             enablePasswordRetrieval="false" 
             enablePasswordReset="true" 
             requiresQuestionAndAnswer="false" 
             requiresUniqueEmail="false" 
             maxInvalidPasswordAttempts="5" 
             minRequiredPasswordLength="6" 
             minRequiredNonalphanumericCharacters="0" 
             passwordAttemptWindow="10" 
             applicationName="/" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" 
             type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" 
             connectionStringName="DefaultConnection" 
             applicationName="/" />
      </providers>
    </roleManager>