DirectoryEntries.Find: “An invalid dn syntax has been specified”

守給你的承諾、 提交于 2019-12-13 14:13:35

问题


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.

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?


回答1:


You have an error in this statemet:

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

I almost sure that it should be: LDAP://CN=Users, instaed of LDAP://CN-Users,

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)

You can find all DC using powershell. Run following command:

New-Object DirectoryServices.DirectoryEntry


来源:https://stackoverflow.com/questions/16168267/directoryentries-find-an-invalid-dn-syntax-has-been-specified

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