由于单引号导致LDAP查询失败 [英] LDAP Query Failure due to single quote

查看:160
本文介绍了由于单引号导致LDAP查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我为姓氏中有一个单引号的员工(例如"O'Connor")运行此命令时,会出现以下错误:

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'失败,没有可用的错误消息,结果代码:DB_E_NOTABLE(0x80040E37).

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

我做错了什么?需要帮助.

What am I doing wrong? need help.

谢谢!

推荐答案

在自己的行上进行替换.

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

这篇关于由于单引号导致LDAP查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆