LDAP Query Failure due to single quote

◇◆丶佛笑我妖孽 提交于 2019-12-13 16:23:23

问题


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!


回答1:


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 + "'";



回答2:


You tried:

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

And not:

Replace("'", "\'")

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



来源:https://stackoverflow.com/questions/497461/ldap-query-failure-due-to-single-quote

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!