How to extend SimpleMembership authentication in ASP.NET MVC4

I've made several ASP.NET MVC sites 1 and 2. Somehow I managed to skip v3. Now I am creating a new ASP.NET MVC 4 site and I can see that all memberships have completely changed.

At first I was happy. It looked easy to customize and use, and the internet project template with all the classes and views I need to get started.

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

My problem is that I cannot find a way to extend SimpleMembership.

Under the old membership provider model, I would take a hit on inheriting from SqlMembership and override ValidateUser () and call the base class or call the web service.

I tried to do something like this by creating a class in my project that inherits from SimpleMembership and then configured it as the default membership provider in web.config, but that just gave me an error

Parser Error Message: This method cannot be called during the application 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>

      

+1


source to share


2 answers


You can expand SimpleMembershipProvider

, but it is not a pleasant experience due to the unusual and restrictive way of initializing providers.

Although providers are initialized by call WebSecurity.InitializeDatabaseConnection

, setting providers through is Web.Config

explicitly fair. If they expand from SimpleMembershipProvider

, they will be configured correctly during initialization.

In fact, you can just implement your own ExtendedMembershipProvider

for use with the API WebSecurity

and not call at all WebSecurity.InitializeDatabaseConnection

(this is only for based implementations SimpleMembershipProvider

). The downside is that it is a lot of work to create this from scratch.



The error you were receiving was probably caused by interacting with vendors early in development, possibly because you were doing something as part of a Initialize

vendor procedure .

For details on the differences between SimpleMembershipProvider

and MembershipProvider

see SimpleMembershipProvider vs MembershipProvider . Take a look at BetterMembership.Net for an example extension SimpleMembershipProvider

.

+1


source


SimpleMembership

not configured in web.config

, it is done in the file InitializeSimpleMembershipAttribute.cs

.

Unfortunately, the implementation of the WebSecurity class used cannot be extended, it hardcodes the use of SimpleMembershipProvider.



You don't need to use SimpleMembership. You can revert to using the old membership provider infrastructure.

+3


source







All Articles