Membership.Provider for mysql in asp.net issue

2019-05-16 05:25发布

问题:

Using MVC4 synced to a mysql server. Did it successfully by changing the machine.config and successfully used the ASP.net Configuration Tool to create users. However when I log in with a username that I created using the tool (and is in the database) I get this error

System.InvalidOperationException was unhandled by user code
Message=To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".
Source=WebMatrix.WebData
StackTrace:
   at WebMatrix.WebData.WebSecurity.VerifyProvider()
   at WebMatrix.WebData.WebSecurity.Login(String userName, String password, Boolean persistCookie)
   at KHSWebsite.Controllers.AccountController.Login(LoginModel model, String returnUrl) in c:\users\alex\documents\visual studio 2010\Projects\KHSWebsite\KHSWebsite\Controllers\AccountController.cs:line 38
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException:

This is a fresh database with a fresh MVC4 Razor web site from VS2010. Any thoughts?

UPDATE: Here is my Web.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 </configSections>
 <connectionStrings>
   <remove name="LocalMySqlServer"/>
   <add name="LocalMySqlServer"
     connectionString="Datasource=localhost;Database=KHS;uid=root;pwd=power1739;"
     providerName="MySql.Data.MySqlClient"/>
 </connectionStrings>
 <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>
    <roleManager defaultProvider="MySQLRoleProvider"/>
    <membership defaultProvider="MySQLMembershipProvider" />
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
     <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <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>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>

回答1:

Asp.Net MVC simple membership is not the same as normal web forms Membership provider. Check following blog post. From linked article

  • SimpleMembership integrates with the previous membership system, but you can't use a MembershipProvider with SimpleMembership
  • ASP.NET 4.5 Web Forms does not use SimpleMembership; it implements OAuth on top of ASP.NET Membership

See also following image:

From exception, it is understood that your provider is classic membership provider and it is not compatible with Simple Membership.

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

You can look to following post1,post2 for MySql compatible Asp.Net MVC Simple Membership provider.



回答2:

You define defaultProvider for roleManager and membership but you must also add providers for them. like this

    <membership defaultProvider="MySqlMembershipProvider">
        <providers>
            <clear/>
            <add name="MySqlMembershipProvider"
                  type="MySql.Web.Security.MySQLMembershipProvider, mysql.web"
                    connectionStringName="LocalMySqlServer"
                    enablePasswordRetrieval="false"
                    enablePasswordReset="true"
                    requiresQuestionAndAnswer="false"
                    requiresUniqueEmail="true"
                    passwordFormat="Hashed"
                    maxInvalidPasswordAttempts="5"
                    minRequiredPasswordLength="6"
                    minRequiredNonalphanumericCharacters="0"
                    passwordAttemptWindow="10"

                    applicationName="/"
                    autogenerateschema="true"/>
        </providers>
    </membership>

    <roleManager enabled="true" defaultProvider="MySqlRoleProvider">
        <providers>
            <clear />
            <add connectionStringName="LocalMySqlServer"
                applicationName="/"
                name="MySqlRoleProvider"
                type="MySql.Web.Security.MySQLRoleProvider, mysql.web"
                autogenerateschema="true"/>
        </providers>
    </roleManager>

Look at this for more information.