Question

I've done several ASP.NET MVC 1 and 2 sites. Somehow I managed to skip v3. I'm now creating a new ASP.NET MVC 4 site and I see that all the Membership stuff has changed completely.

At first I was happy. It looked easy to setup and use and the Internet project template came with all the classes and views I needed to get started.

Unfortunately, I'm now completely stuck. I need extend the code that authenticates a user. Basically, I need to authenticate some users against a web service and other users against the local database. Both types of users will have profile data stored in the local database.

My problem is that I can't find any way extending SimpleMembership.

Under the old Membership provider model, I would have taken a stab at inheriting from SqlMembership and overriding ValidateUser() and calling the base class or calling the web service.

I did try doing something like that my creating a class in my project that inherites from SimpleMembership and then configuring it as the default membership provider in web.config, but that just gave me the error

Parser Error Message: This method cannot be called during the application's pre-start initialization phase.

Line 32:     <membership defaultProvider="ssund">
Line 33:       <providers>
Line 34:         <add name="ssund" type="Ssund.Web.Providers.SsundSimpleMembershipProvider"/>
Line 35:       </providers>
Line 36:     </membership>
Was it helpful?

Solution

You can extend SimpleMembershipProvider but it's not a pleasant experience due to the unusual and restrictive way the providers are initialized.

Although the providers are initialized via a call to WebSecurity.InitializeDatabaseConnection, setting the providers via the Web.Config explicitly is just as valid. Provided they extend from SimpleMembershipProvider they will be configured correctly during initialization.

In fact you could just implement your own ExtendedMembershipProvider for use with WebSecurity APIs, and not call WebSecurity.InitializeDatabaseConnection at all (it's only for SimpleMembershipProvider based implementations). The downside is that's is a lot of work to build this from scratch.

The error you were getting was probably caused by interaction with the providers to early in the pipeline, possibly because you were doing something within the Initialize routine of the provider.

See SimpleMembershipProvider vs MembershipProvider for more information of the differences between SimpleMembershipProvider and MembershipProvider. And take a look at BetterMembership.Net for an example of extending the SimpleMembershipProvider.

OTHER TIPS

SimpleMembership is not configured in the web.config, it's done in the InitializeSimpleMembershipAttribute.cs file.

Unfortunately, the implementation of the WebSecurity class that's used cannot be extended, it hard codes the use of SimpleMembershipProvider.

You don't have to use SimpleMembership. You can fall back to using the old membership provider infrastructure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top