Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Membership.Provider" property must be an instance of "ExtendedMembershipProvider"

I am using SimpleMembership for an ASP.NET MVC4 based application. I have setup authentication mechanism by following few blogs out there on internet. Just for a background following is highlights:

• I have a class “InitializeSimpleMembershipAttribute” that initialize the membership. Ensure this is called before first call to any method of WebSecurity class.

• I have a SeedData class that creates default users and roles utilizing WebSecurity successfully (DB gets default data).

• The application in its current states lets user login and use the functionality that he has access to. Hence I think we can say SimpleMembership is setup correctly and working.

• Now I am adding “ResetPassword” functionality. I have followed few blogs e.g.

http://www.itorian.com/2013/03/PasswordResetting.html#comment-form and http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.resetpassword(v=vs.111).aspx

I have implemented basic stuff (view and logic), however when I make a call to WebSecurity.GeneratePasswordResetToken(userName);

I get following exception.

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider". Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

I see in ILSpy that this is coming from WebSecurity.VerifyProvider() method. However I can not figure out why?

ExtendedMembershipProvider extendedMembershipProvider = Membership.Provider as ExtendedMembershipProvider;

Above line from WebSecurity class must be returning null to cause this exception however it should not be as the configuration exist in web.config file and I am nowhere using any other provider that is not extended from the "ExtendedMembershipProvider".

Let me show you my web.config which is a little modified based on suggestion people made for resolving this issue.

    <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" />
    <add key="enableSimpleMembership" value="true" />
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="true" targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider" lockItem="true">
      <providers>
        <clear />
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
    <membership defaultProvider="SimpleMembershipProvider" lockItem="true">
      <providers>
        <clear />
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <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="PublicWebsiteDbContext" />
      </providers>
    </sessionState>
    <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="PublicWebsiteDbContext" applicationName="/" />
      </providers>
    </profile>
  </system.web>

Try 1: Set lockItem="true" on the attributes above to ensure global configuration is not overriding my configuration.

Try 2: Ensured necessary membership configuration is present in the “web.config” as suggested in the answer for below question: To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider"

Try 3: Added <add key="enableSimpleMembership" value="true" /> in web.config file.

Try 4: Have read at least top 10 help link on google and 5 answer on SO that seems relevant on this topic e.g. To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider" Actually did not understand how he got the error resolved with reason mentioned in the answer. “ActionExecutingContext” is not in the “System.Web.Http”.

Can any one of you kindly suggest other things I can try out to get rid of this exception please?

I know this is ought to work for me as well just annoyed with time >2 hours I had to waste resolving such issue (at the point scrap using SimpleMembership and use my own authentication mechanism).

UPDATE: The problem is solved. The change which solved this exception is, I was referencing WebMatrix.WebData assembly version 1.0 in my Business layer project, i replaced it with assembly version 2.0. I have version 2.0 assembly used in other projects assemblies (DAL) as well.

I compared both version of WebMatrix.WebData assembly however could not found any difference in code that's in question. So not sure exactly why it was failing to cast the provider to "ExtendedMembershipProvider"?

Could it be related to since other project especially web project is loading version 2.0 and the business layer which is making WebSecurity.GeneratePasswordResetToken looking for 1.0 assembly, it might be causing issue? In that case should I not get a warning or other help from exception?

Thanks in advance > Shailendra

like image 426
SBirthare Avatar asked Mar 22 '23 21:03

SBirthare


1 Answers

The change which solved this exception is, I was referencing WebMatrix.WebData assembly version 1.0 in my Business layer project, i replaced it with assembly version 2.0.

When I compared both version of WebMatrix.WebData assemblies I could not found any difference in code that's in question.

However the fact that I have version 2.0 assembly already used in one other project assembly (DAL) as well.

So while I am not 100% sure my theory is, at runtime while checking the provider it was failing to load correct assembly due to conflict (one version was already loaded which is referenced in DAL project and when second project method is called it referenced a different version of same assembly) and it is showing generic exception message.

like image 134
SBirthare Avatar answered Apr 09 '23 22:04

SBirthare