DirectoryEntries.Find:“指定了无效的dn语法”

时间:2013-04-23 11:31:22

标签: c# active-directory ldap

我正在尝试在当前域中找到用户。代码是这样的:

DirectoryEntry domain = new DirectoryEntry("LDAP://CN-Users, DC=" + Environment.UserDomainName);
            DirectoryEntries entries = domain.Children;
            try
            {
                // The following line causes the exception
                DirectoryEntry user = entries.Find("(&(objectCategory=user)(cn=" + userName + "))", ActiveDirectoryEntryType.User.TypeName);
                user.DeleteTree();
                user.CommitChanges();
            }
            catch
            {}

我收到了一个错误:

An invalid dn syntax has been specified.

我也尝试了以下代码并得到了同样的错误:

DirectoryEntry user = entries.Find(userName, ActiveDirectoryEntryType.User.TypeName);

我找不到有关帮助文件中正确语法的信息。有谁知道这是怎么做的?

1 个答案:

答案 0 :(得分:4)

您在此状态中遇到错误:

DirectoryEntry domain = new DirectoryEntry("LDAP://CN-Users, DC=" + Environment.UserDomainName);

我几乎可以肯定:LDAP://CN=Users,已成为LDAP://CN-Users,

第二件事是DC=" + Environment.UserDomainName,这可能是错误的,因为通常它是这样的:LDAP://OU=Finance,dc=fabrikam,dc=com(有多个DC)

您可以使用powershell找到所有DC。运行以下命令:

New-Object DirectoryServices.DirectoryEntry