在asp.net问题中为mysql提供Membership.Provider

时间:2013-05-19 13:31:40

标签: c# asp.net mysql visual-studio-2010 razor

使用MVC4同步到mysql服务器。是否成功通过更改machine.config并成功使用ASP.net配置工具来创建用户。但是,当我使用我使用该工具创建的用户名登录(并且在数据库中)时,我收到此错误

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:

这是一个全新的数据库,其中包含VS2010的全新MVC4 Razor网站。有什么想法吗?

更新: 这是我的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>

2 个答案:

答案 0 :(得分:3)

Asp.Net MVC简单会员资格与普通网络表单会员提供商不同。检查blog post。来自链接文章

  • SimpleMembership与之前的会员系统集成,但您不能将MembershipProvider与SimpleMembership一起使用
  • ASP.NET 4.5 Web窗体不使用SimpleMembership;它在ASP.NET Membership
  • 之上实现OAuth

另见以下图片: Membership Provider Class Diagram

从例外情况来看,您的提供商是classic membership provider并且与简单会员资格不兼容。

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

对于MySql兼容的Asp.Net MVC简单成员资格提供程序,您可以查看以下post1post2

答案 1 :(得分:2)

您为defaultProviderroleManager定义membership,但您还必须为其添加提供商。像这样

    <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>

请查看this以获取更多信息。