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

I am using SimpleMembership for an ASP.NET MVC4 based application. I have an authentication mechanism by following several blogs on the Internet. Just for the background follow the main points:

β€’ I have a class "InitializeSimpleMembershipAttribute" that initializes membership. Make sure that this is called before the first call to any method in the WebSecurity class.

β€’ I have a SeedData class that creates default users and roles using WebSecurity successfully (DB gets the default data).

β€’ The application in its current states allows the user to log into the system and use the functions to which he has access. Therefore, I think that we can say that SimpleMembership is properly configured and working.

β€’ Now I add the functionality of "ResetPassword". I followed several blogs, for example.

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

I implemented the basic things (browsing and logic), however, when I call WebSecurity.GeneratePasswordResetToken (Username);

I get the 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. View the stack trace for more information about the error and its occurrence in the code. Exception Details: System.InvalidOperationException: To call this method, the Membership.Provider property must be an instance of ExtendedMembershipProvider.

In ILSpy, I see that this comes from the WebSecurity.VerifyProvider () method. However, I can’t understand why?

ExtendedMembershipProvider extendedMembershipProvider = Membership.Provider as ExtendedMembershipProvider; 

Above the line from the WebSecurity class, null must be returned to cause this exception, however it should not be the same as the configuration exists in the web.config file, and I will not use another provider that is not distributed from "ExtendedMembershipProvider" anywhere.

Let me show you my web.config file, which is slightly modified based on the suggestions made to solve this problem.

  <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" in the above attributes so that the global configuration does not overlap my configuration.

Try 2: Ensure that the required membership configuration is present in "web.config", as suggested in the answer to the following question: To call this method, the "Memberhip.Provider" property must be an instance of "ExtendedMembershipProvider"

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

Try 4: Read at least 10 of the best links to Google and 5 answers to SO that seem relevant on this, for example. To call this method, the "Memberhip.Provider" property must be an instance of the "ExtendedMembershipProvider" . I really did not understand how he got the error resolved for the reason mentioned in the answer. "ActionExecutingContext" is not located in "System.Web.Http".

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

I know that this should work for me, but it was just annoying over time> 2 hours when I had to spend time solving this problem (in a point lot using SimpleMembership and using my own authentication mechanism).

UPDATE: Problem resolved. The change that allowed this exception, I referenced the assembly WebMatrix.WebData version 1.0 in my project "Business Layer", I replaced it with the version of the assembly 2.0. I have a build version 2.0 used in other projects (DAL).

I compared both versions of the WebMatrix.WebData assembly, however I did not find any difference in the code that is in the question. Therefore, it is not entirely accurate why he did not provide the provider "ExtendedMembershipProvider"?

Maybe this is due to the fact that another project, especially a web project, is loading version 2.0 and the business layer that WebSecurity.GeneratePasswordResetToken does. WebSecurity.GeneratePasswordResetToken looking for assembly 1.0, can this cause problems? In this case, I should not receive a warning or other help from the exception?

Thanks in advance> Shailendra

+4
source share
2 answers

The change that allowed this exception, I referenced the assembly WebMatrix.WebData version 1.0 in my project "Business Layer", I replaced it with version 2.0 of the assembly.

When I compared both versions of the WebMatrix.WebData assembly, I could not find the difference in the code that is in the question.

However, the fact that I have a build of version 2.0 is already being used in another build of the project (DAL).

Thus, although I am not 100% sure that my theory is at runtime when checking the provider, it could not load the correct assembly due to a conflict (one version was already loaded that the DAL project refers to and when the second project method is called he referenced another version of the same assembly), and it shows a general exception message.

+4
source

I had to remove all WebMatrix.Data and WebMatrix.WebData links in ALL of my projects, adding them back using Copy Local = true, and it all worked. Make sure you have the correct version number! v2.something.

+3
source

Source: https://habr.com/ru/post/1495266/


All Articles