Reputation: 14494
I have a intranet that needs to run queries for users in the AD system. I'm trying to connect using a DirectoryEntry() object, then setup a DirectorySearcher() and do a search for users. Boiled down to it's most basic, my code is like this:
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://RD-HQ/CN=Users,DC=rd-hq,DC=local");
DirectorySearcher ds = new DirectorySearcher(directoryObject);
ds.Filter = ("(&(objectClass=user))");
var test = ds.FindAll();
foreach (SearchResult item in test)
{
}
Weirdly, this works on my development machine when I reference the .NET4.0 version of the System.DirectoryServices DLL, but not in .NET3.5 (where the version of the System.DirectoryServices DLL is v2.0.0.0).
The specific error I get is on the ds.FindAll()
method and reads:
Exception Details: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.
Stepping through the code reveals that the directoryObject
object is actually not being initialised correctly on the first line. The constructor doesn't fail, but all the properties are throwing exceptions.
Any ideas what I might be doing wrong? Thanks, I realise this is a bit of a crappy, vague question.
SOLVED
OK, I managed to fix this by using the overload for the DirectoryEntry() constructor that accepted a username and password:
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://RD-HQ/CN=Users,DC=rd-hq,DC=local", "rd-hq.local\mick", "notmypassword");
I'm still not sure why this works differently in the .NET3.5 version versus the .NET4.0 version and will happily mark as correct the answer that can at least help me understand this stuff better.
Upvotes: 2
Views: 2097
Reputation: 124794
The constructor doesn't fail, but all the properties are throwing exceptions.
The DirectoryEntry
instance will attempt to bind to the directory when you access one of the relevant properties - if this fails the property accessor will throw an exception. This also happens if you attempt to view the properties from the debugger.
In your above code, the bind happens when ds.FindAll()
is called.
Clearly there is a problem binding to the directory.
I find it difficult to believe that the .NET Framework version makes a difference; I would strip your code down to a bare-bones sample you're using identical code and an identical environment for the .NET 3.5 and .NET 4 versions.
The error message "An operations error occurred" is rather general, but can happen if you don't have permission to access the directory. So one possibility is that your .NET 3.5 and .NET 4 versions are running under different identities. At least that appears to have been the cause of this similar problem.
The ErrorCode returned by the DirectoryCOMException might also give a clue.
Upvotes: 4