1

I'm having a problem with a custom Membership implementation for ASP.NET using EF. The thing is, my web.config features the default values for the provider but at runtime those default are only being read in Initialize() the first time provider is instantiated (that happens for instance when I request the Register view via GET but when I fill in the form and POST to the controller the Initialize() on the provider class doesn't get called and all the provider settings are null or default value types value.

This is my web.config section:

    <membership>
        <providers>
            <clear />
            <add name="AspNetSqlMembershipProvider"
                 type="BN.Membership.EfMembershipProvider, BN"
                 connectionStringName="ApplicationServices"
                 enablePasswordRetrieval="false"
                 enablePasswordReset="true"
                 requiresQuestionAndAnswer="false"
                 requiresUniqueEmail="true"
                 maxInvalidPasswordAttempts="5"
                 minRequiredPasswordLength="6"
                 minRequiredNonalphanumericCharacters="0"
                 passwordAttemptWindow="10"
                 applicationName="/" />
        </providers>
    </membership>
    <roleManager enabled="false">
        <providers>
            <clear />
            <add name="AspNetSqlRoleProvider"
                 type="BN.Membership.EfRoleProvider, BN"
                 connectionStringName="ApplicationServices"
                 applicationName="/" />
        </providers>
    </roleManager>

Since my provider class is quite huge I am not posting it here but will post some parts of it if requested.

mare
  • 13,033
  • 24
  • 102
  • 191
  • Does this help: http://stackoverflow.com/questions/623545/how-do-i-call-initialize-on-a-custom-membershipprovider - Posting the code that initializes the provider in the post would help. – John Farrell Oct 22 '10 at 17:02
  • Thanks, your comment helped in a way that I realized I need to get the instance from the Membership.Providers collection but this requires parameterless constructor. I forgot to mention that I use Ninject and as such have apparently ran into this problem http://stackoverflow.com/questions/2753424/dependency-injection-with-custom-membership-provider – mare Oct 22 '10 at 18:27
  • It seems I'll have to use this http://commonservicelocator.codeplex.com/ ..Do you use it in your projects? – mare Oct 22 '10 at 18:48
  • You have a misconception about how membership providers work. They are only instantiated once. Membership is a static class, which means it lives for the life of app, so initialize will only ever be called once. The provider, once initialized, is stored in a member of the Membership class and is referenced from there. This is one reason why Membership providers and Dependency Injection don't get along. You have to jump through some serious hoops to make DI work with Membership providers. – Erik Funkenbusch Oct 03 '12 at 04:20
  • By the way, you may be interested in this: http://www.planetgeek.ch/2012/02/08/asp-net-provider-injection-with-ninject-3-0-0/ – Erik Funkenbusch Oct 03 '12 at 04:25
  • thanks for the comments Mystere Man, I am about to rework it to move away from Membership Providers and use REST membership services like the ServiceStack authentication service – mare Oct 11 '12 at 11:23

1 Answers1

0

Does this help: How do I call Initialize on a custom MembershipProvider? - Posting the code that initializes the provider in the post would be helpful.


You don't HAVE to use a CSL pattern to use Ninject with this. CSLs are semi-useful though although I have no idea why you'd choose to change your DI framework in the middle of a project.

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • I added CSL and I sort of like it. I like having some common ground for accessing instances of types provided by the chosen DI framework. – mare Oct 24 '10 at 15:04