Wednesday, December 05, 2007

DirectoryEntry: "Domain not found or cannot bind"

Be careful using System.DirectoryServices.DirectoryEntry() in a Web Service.

Even if user and password are specified in constructor, the connection somatimes cnnot be established.

DirectoryEntry de = new DirectoryEntry("LDAP://dc=abcde,dc=fgh","ADuser","ADpwd");

string sName = de.Name, // <-- Exception thrown

Same code may run without problems as console application, but stuck on thrown exception in Web Service.
The reason is: the service account used for Web Service cannot resolve domain name and detect DC address.

Workaround: resolve DC address and put it into LDAP connection string:

DirectoryEntry de = new DirectoryEntry("LDAP://dcMachineNameOrIPAddress/dc=abcde,dc=fgh","ADuser","ADpwd");

string sName = de.Name, // Works now

Enjoy!