【问题标题】:C# MySQL SImple Membership ProviderC# MySQL 简单成员资格提供程序
【发布时间】:2016-02-27 01:41:49
【问题描述】:

我正在使用 Microsoft Visual Studio 2015(社区版)构建一个简单的 c# Web 应用程序(不是 MVC)。我的数据库是最新版本的 MySQL,我安装的 MySQL 连接器也是。

我已修改我的 Web.config 文件以使用 WebMatrix Simple Membership Provider 和 MySQL。

这是我的 Web.config 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth.Core">
      <section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
      <section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
      <section name="oauth" type="DotNetOpenAuth.Configuration.OAuthElement, DotNetOpenAuth.OAuth" requirePermission="false" allowLocation="true" />
      <section name="openid" type="DotNetOpenAuth.Configuration.OpenIdElement, DotNetOpenAuth.OpenId" requirePermission="false" allowLocation="true" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D" />
        <add assembly="MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2" />
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear />
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <profile defaultProvider="MySQLProfileProvider">
      <providers>
        <remove name="MySQLProfileProvider" />
        <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear/>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
    <machineKey validationKey="AutoGenerate" validation="SHA1"/>
    <siteMap defaultProvider="MySqlSiteMapProvider">
      <providers>
        <remove name="MySqlSiteMapProvider" />
        <add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
      </providers>
    </siteMap>
    <webParts>
      <personalization defaultProvider="MySQLPersonalizationProvider">
        <providers>
          <remove name="MySQLPersonalizationProvider" />
          <add name="MySQLPersonalizationProvider" type="MySql.Web.Personalization.MySqlPersonalizationProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
        </providers>
      </personalization>
    </webParts>
    <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="DefaultConnection" />
      </providers>
    </sessionState>
    <customErrors mode="Off" />
    <trust level="Full" />
  </system.web>
  <connectionStrings>
    <remove name="LocalMySqlServer"/>
    <add name="RP" connectionString="Server=localhost; Database=my_db; uid=my_uid; pwd=my_password;" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebMatrix.WebData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
    <!-- This prevents the Windows Event Log from frequently logging that HMAC1 is being used (when the other party needs it). -->
    <legacyHMACWarning enabled="0" />
    <!-- When targeting ASP.NET MVC 3, this assemblyBinding makes MVC 1 and 2 references relink
         to MVC 3 so libraries such as DotNetOpenAuth that compile against MVC 1 will work with it.
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
     -->
  </runtime>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </entityFramework>
  <system.net>
    <defaultProxy enabled="true" />
    <settings>
      <!-- This setting causes .NET to check certificate revocation lists (CRL) 
           before trusting HTTPS certificates.  But this setting tends to not 
           be allowed in shared hosting environments. -->
      <!--<servicePointManager checkCertificateRevocationList="true"/>-->
    </settings>
  </system.net>
  <dotNetOpenAuth>
    <messaging>
      <untrustedWebRequest>
        <whitelistHosts>
          <!-- Uncomment to enable communication with localhost (should generally not activate in production!) -->
          <!--<add name="localhost" />-->
        </whitelistHosts>
      </untrustedWebRequest>
    </messaging>
    <!-- Allow DotNetOpenAuth to publish usage statistics to library authors to improve the library. -->
    <reporting enabled="true" />
    <!-- This is an optional configuration section where aspects of dotnetopenauth can be customized. -->
    <!-- For a complete set of configuration options see http://www.dotnetopenauth.net/developers/code-snippets/configuration-options/ -->
    <openid>
      <relyingParty>
        <security requireSsl="false">
          <!-- Uncomment the trustedProviders tag if your relying party should only accept positive assertions from a closed set of OpenID Providers. -->
          <!--<trustedProviders rejectAssertionsFromUntrustedProviders="true">
            <add endpoint="https://www.google.com/accounts/o8/ud" />
          </trustedProviders>-->
        </security>
        <behaviors>
          <!-- The following OPTIONAL behavior allows RPs to use SREG only, but be compatible
               with OPs that use Attribute Exchange (in various formats). -->
          <add type="DotNetOpenAuth.OpenId.RelyingParty.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth.OpenId.RelyingParty" />
        </behaviors>
      </relyingParty>
    </openid>
  </dotNetOpenAuth>
  <uri>
    <!-- The uri section is necessary to turn on .NET 3.5 support for IDN (international domain names),
         which is necessary for OpenID urls with unicode characters in the domain/host name.
         It is also required to put the Uri class into RFC 3986 escaping mode, which OpenID and OAuth require. -->
    <idn enabled="All" />
    <iriParsing enabled="true" />
  </uri>
</configuration>

简单的会员资格提供程序被设置为自动创建会员资格数据库表,它成功地做到了。

我可以从我的数据库中选择数据并显示。

但是,当我去注册用户(使用简单会员提供程序自动创建的网页)时,我收到了错误。

Membership Data,通过关联的 Simple Provider 默认写入两个表,一个 userprofile 表,一个 pages_membership 表。

当我点击“提交”按钮时,数据已正确写入用户配置文件表,但是在将数据写入网页成员表时出现此错误:

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@myurl.com')' at line 1

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: MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@robeyspub.com')' at line 1

Source Error: 


Line 50:                 {
Line 51:                     bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
Line 52:                     var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
Line 53:                     if (requireEmailConfirmation)
Line 54:                     {

Source File: c:\Users\brian\OneDrive\Documents\Visual Studio 2015\Projects\Robey's Pub VS2015 Razor\Account\Register.cshtml    Line: 52 

Stack Trace: 


[MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@robeyspub.com')' at line 1]
   MySql.Data.MySqlClient.MySqlStream.ReadPacket() +272
   MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) +68
   MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) +17
   MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) +110
   MySql.Data.MySqlClient.MySqlDataReader.NextResult() +783
   MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +1564
   MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar() +112
   WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +98
   WebMatrix.WebData.DatabaseWrapper.QueryValue(String commandText, Object[] parameters) +29
   WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase db, String userTableName, String userNameColumn, String userIdColumn, SimpleMembershipProviderCasingBehavior casingBehavior, String userName) +217
   WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +158
   WebMatrix.WebData.WebSecurity.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +51
   ASP._Page_Account_Register_cshtml.Execute() in c:\Users\brian\OneDrive\Documents\Visual Studio 2015\Projects\Robey's Pub VS2015 Razor\Account\Register.cshtml:52
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +177
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +83
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +103
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +101
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +110

我不知道如何解决这个问题。任何帮助表示赞赏。谢谢!


更多信息;

此语句上的程序错误(这是一个自动(计算机)生成的语句,由 Simple Membership Provider 创建:

var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);

以下是错误详情:


 MySql.Data.MySqlClient.MySqlException was unhandled by user code   ErrorCode=-2147467259   

HResult=-2147467259
Message=您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@robeyspub.com')' 附近使用正确的语法 Number= 1064
Source=MySql.Data

堆栈跟踪: 在 MySql.Data.MySqlClient.MySqlStream.ReadPacket() 在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& 影响行,Int64& 插入 ID) 在 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32& 受影响的Rows,Int64& 插入的Id) 在 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,布尔力) 在 MySql.Data.MySqlClient.MySqlDataReader.NextResult() 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为) 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar() 在 WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) 在 WebMatrix.WebData.DatabaseWrapper.QueryValue(字符串命令文本,对象 [] 参数) 在 WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase 数据库,字符串 userTableName,字符串 userNameColumn,字符串 userIdColumn,SimpleMembershipProviderCasingBehavior 套管行为,字符串用户名) 在 WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(字符串用户名,字符串密码,布尔 requireConfirmationToken) 在 WebMatrix.WebData.WebSecurity.CreateAccount(字符串用户名,字符串密码,布尔 requireConfirmationToken) 在 c:\Users\brian\OneDrive\Documents\Visual Studio 2015\Projects\Robey's Pub VS2015 Razor\Account\Register.cshtml 中的 ASP._Page_Account_Register_cshtml.Execute():第 52 行 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 在 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 执行器) 在 System.Web.WebPages.WebPage.ExecutePageHierarchy() 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage) 在 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)

InnerException:

【问题讨论】:

  • 我目前正在我的项目中用 MySQL 替换 SQL Server,并且遇到了同样的问题。你能解决它吗?

标签: c# mysql asp.net webmatrix


【解决方案1】:

您的查询有误 这里 请检查您的查询一次 '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@robeyspub.com')'

检查你是否使用了单引号

这个 '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER(' 这将是第一个字符串 这将是第二个 ')'

但是系统解释了什么 brian@robeyspub.com

使用调试器做一件事并检查您的查询并将其运行到 mysql 中,您将得到您遇到的错误 谢谢

【讨论】:

  • 问题是...这不是“我的”查询...该查询源自与简单成员资格提供程序关联的 DLL。我无法以任何方式更改查询。
  • 显然你的错误是说你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '[UserId] FROM [UserProfile] WHERE (UPPER([Email]) = UPPER('brian@robeyspub.com')' 附近使用正确的语法]
  • 我有错误,但我无法将其发布到评论中,因为它说我的评论中有“太多字符”。我不确定如何发布我的错误...
  • @BrianAshcraft 您可以将该信息编辑到您的问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
相关资源
最近更新 更多