MySQL 中似乎存在一个错误,如果用户名包含斜杠,则 Roles.RemoveUserFromRole 不起作用。该问题在这里有详细描述https://bugs.mysql.com/bug.php?id=77264但我似乎找不到任何解决方法。我尝试使用 userName.Replace(@"\", @"\") 转义“\”,但这没有什么区别。有什么建议吗?
最佳答案
我最终只是自己动手,直到这个问题在 MySQL 角色提供程序中得到解决。如果有人需要的话,这是代码(nHibernate 风格)。
private static bool RemoveUserFromRole(string userId, string roleName)
{
var sess = NHibernateSession.GetDefaultSessionFactory().OpenSession();
const string query = @"
delete
from my_aspnet_usersinroles
where
roleId in (select id from my_aspnet_roles where name = :roleName)
and userId = :userId;
";
var result = sess.CreateSQLQuery(query)
.SetParameter("userId", userId)
.SetParameter("roleName", roleName)
.ExecuteUpdate();
return result >= 0;
}
关于MySQL Roles.RemoveUserFromRole 在 MySQL 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603035/