由于单引号导致 LDAP 查询失败

发布于 2024-07-13 04:12:03 字数 530 浏览 3 评论 0原文

我正在尝试使用以下 c# 代码在 Active Directory 中查找员工:

"Select userPrincipalName, ADsPath, Department, Mail,
 HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "'
WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName.Replace("'", "''") + "'";

当我为姓氏中带有单引号的员工(例如“O'Connor”)运行此代码时,我收到以下错误:

AdsDsoObject' 失败,没有可用的错误消息,结果代码:DB_E_NOTABLE(0x80040E37)。

I also tried Replace("'", "\''"), nothing is working. 

我做错了什么? 需要帮忙。

谢谢你!

I'm trying to find an employee in Active Directory using the following c# code:

"Select userPrincipalName, ADsPath, Department, Mail,
 HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "'
WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName.Replace("'", "''") + "'";

When I run this for an employee with a single quote in the last name (such as "O'Connor") I get the following error:

AdsDsoObject' failed with no error message available, result code: DB_E_NOTABLE(0x80040E37).

I also tried Replace("'", "\''"), nothing is working. 

What am I doing wrong? need help.

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2024-07-20 04:12:03

在它自己的线路上进行替换。

Username.Replace("'", "\'");

"Select userPrincipalName, ADsPath, Department, Mail, HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "' WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName + "'";

Do the replace on it's own line.

Username.Replace("'", "\'");

"Select userPrincipalName, ADsPath, Department, Mail, HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "' WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName + "'";
〆凄凉。 2024-07-20 04:12:03

您尝试过:

Replace("'", "\''")

而不是:(

Replace("'", "\'")

其中有一个额外的单引号)。

You tried:

Replace("'", "\''")

And not:

Replace("'", "\'")

(There's an extra single quote in there).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文