Webmatrix.WebData.WebSecurity 和 MySql 无效。在 WebSecurity.UserExists() 检查中抛出 sql 语法错误

Webmatrix.WebData.WebSecurity with MySql is not working. Throwing sql syntax error in WebSecurity.UserExists() check

我在 MSSQL 中完成了一个项目。我将 MSSQL 迁移到 MySql,因为我认为 Webmatrix.WebData 也将支持 MySql。 WebSecurity 初始化成功。

if (!WebSecurity.Initialized)
     {
          securityService.Initialize(
          "sqlserver",
          "UserProfile",
          "UserId"
          "UserName");
      };` 

以上代码执行成功。但是如果下面的部分执行失败,

 if (!WebSecurity.UserExists("user"))
        {
            WebSecurity.CreateUserAndAccount("user", "password");
        }

检查 WebSecurity.UserExists("user") 失败。这是错误信息-

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code

Additional information: 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 [UserTable] WHERE (UPPER([UserName]) = UPPER('user'))' at line 1

请帮我解决这个问题。

ASP.Net 附带的 WebSecurity(上面代码中使用的那个)在代码中嵌入了一些 Microsoft T-SQL 语法。 MySQL 语法和 MSSQL 之间几乎没有区别,这可能是错误的原因。

从显示的错误中,我可以看到 WebSecurity 中的 CreateUserAndAccount 方法以某种方式尝试执行 T-SQL 语句 .... [UserId] FROM [UserTable] WHERE (UPPER([UserName]) = UPPER('user')) 然而,虽然这在 MSSQL,方括号 "[""]" 会导致 MySQL.

中的错误

解决方法是使用 MySql.Web.Security.MySqlWebSecurity.CreateUserAndAccount("user", "password"),当然,您可以添加 using 语句或以这种方式使用它。

see link