I have a simple setup below to search for users.
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://someserver:123/OU=d-users,DC=domain,DC=x,DC=y,DC=com");
rootEntry.AuthenticationType = AuthenticationTypes.None;
string filter = "sAMAccountName=" + AccountName;
DirectorySearcher searcher = new DirectorySearcher(rootEntry, filter);
SearchResult foundUser = searcher.FindOne();
For some reason I can search via a simple Console/windows forms app but cannot search from a wcf or asmx service (v4.0, Integrated) on IIS(6.1). The exception is below;
exception:System.Runtime.InteropServices.COMException (0x8007200A): The specified directory service attribute or value does not exist.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at Tester.FindAccountByName(String AccountName)
Try to put your filter into brackets:
string filter = string.Format("(sAMAccountName={0})", AccountName);
See the relevant TechNet article on LDAP filter syntax for more details
Try using an DirectorySearcher, something like this:
using (DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("LDAP://someserver:123/OU=d-users,DC=domain,DC=x,DC=y,DC=com")))
{
StringBuilder filterStringBuilder = new StringBuilder();
// Just create a single LDAP query for all user SIDs
filterStringBuilder.Append("(&(objectClass=user)(|");
filterStringBuilder.AppendFormat("({0}={1})", "sAMAccountName", AccountName);
filterStringBuilder.Append("))");
searcher.PageSize = 1000; // Very important to have it here. Otherwise you'll get only 1000 at all. Please refere to DirectorySearcher documentation
searcher.Filter = filterStringBuilder.ToString();
searcher.ReferralChasing = ReferralChasingOption.None;
searcher.PropertiesToLoad.AddRange(
new[] { "DistinguishedName" });
var result = searcher.FindOne();
}
More examples on how to use the DirectorySearcher you can find on msdn or stackoverflow
User contributions licensed under CC BY-SA 3.0