DirectoryEntries.Find:"一个无效的dn语法已指定"
我试图找出在当前域中的用户。在code是这样的:
I'm trying to find a user in the current domain. The code is this:
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
{}
我得到一个错误:
I'm getting an error:
An invalid dn syntax has been specified.
我也试过以下code,并得到了同样的错误:
I also tried the following code and got the same error:
DirectoryEntry user = entries.Find(userName, ActiveDirectoryEntryType.User.TypeName);
我无法找到有关的帮助文件的正确语法的信息。有谁知道如何做到这一点?
I could not find information about the proper syntax in the help files. Does anyone know how this is done?
您在本statemet错误:
You have an error in this statemet:
DirectoryEntry domain = new DirectoryEntry("LDAP://CN-Users, DC=" + Environment.UserDomainName);
我几乎可以肯定,它应该是: LDAP:// CN =用户,
instaed 的LDAP:// CN-用户,
I almost sure that it should be: LDAP://CN=Users,
instaed of LDAP://CN-Users,
第二件事是 DC =+ Environment.UserDomainName
这可能错了,因为通常游戏是这样的: LDAP:// OU =金融,DC = Fabrikam目录,DC = COM
(有多个DC)
Second thing is DC=" + Environment.UserDomainName
which maybe wrong, because ususally it is something like this: LDAP://OU=Finance,dc=fabrikam,dc=com
(there is more than one DC)
您可以使用PowerShell的找到所有的DC。运行以下命令:
You can find all DC using powershell. Run following command:
New-Object DirectoryServices.DirectoryEntry