user1943020
user1943020

Reputation:

Setting up SimpleMembership in MVC4

I am reading that in MVC4 to set up simple membership I should do this step:

In the AppSettings include a line:

<add key="enableSimpleMembership" value="true" />

However when I look at the samples generated from the templates they only have:

  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

So why do I keep reading it's necessary to set the enableSimpleMembership key?

Upvotes: 7

Views: 8724

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

By default the SimpleMembershipProvider is enabled when you create a new ASP.NET MVC 4 application. But some hosting providers might disable it by overriding this setting in a higher level web.config.

Quote from an article about SimpleMembership:

If you see an error that tells you that a property must be an instance of ExtendedMembershipProvider, the site might not be configured to use the ASP.NET Web Pages membership system (SimpleMembership). This can sometimes occur if a hosting provider's server is configured differently than your local server. To fix this, add the following element to the site's Web.config file:

<appSettings>

   <add key="enableSimpleMembership" value="true" />

</appSettings>

This setting is used by the WebMatrix.WebData.PreApplicationStartCode method which executes automatically when your site runs and will use the value of this setting to enable the simple membership provider.

Actually configuring the SimpleMembershipProvider explicitly is what I would recommend you:

<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <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>

Now, there's no room for confusion anymore. Both the membership and role providers are configured explicitly.

Upvotes: 9

Related Questions