How to find an user on a LDAP server

3

When trying to find an User on a LDAP Server, I get the following error "Unknown error (0x8000500c)"

This is the code I'm using:

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "gandalf.intrafg");
UserPrincipal p = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Consultor1");

Indidentally, the following piece of code seems to work (no exception is generated), but the samAccountName comes through as a byte array. Anybody knows why?

DirectoryEntry entry = new DirectoryEntry("LDAP://gandalf.intrafg");

DirectorySearcher searcher = new DirectorySearcher(entry);

//searcher.PropertiesToLoad.Add("givenName");
//searcher.PropertiesToLoad.Add("sn");
searcher.PropertiesToLoad.Add("samAccountName");

searcher.Filter = "(&(objectCategory=person)(samAccountName=Consultor1))";

SearchResult result = searcher.FindOne();
c#
ldap
asked on Stack Overflow Sep 9, 2010 by bastos.sergio • edited Sep 9, 2010 by bastos.sergio

2 Answers

0

Your second code block works just fine, I however did not pass the domain name in the DirectoryEntry initializer.

    Directory entry = new DirectoryEntry();
//other code
result.Properties["samAccountName"][0].ToString();
answered on Stack Overflow Sep 9, 2010 by Ta01
0

The code you have should be fine - it works for me, no problem at all.

However: you're not telling us what you fill in for domain_name here:

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "domain_name");

or userId here:

UserPrincipal p = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, UserId);

The domain_name must be in the "old" NetBIOS style, e.g. FABRIKAM - no DNS-style like fabrikam.com or AD-style like dc=fabrikom,dc=com or even a full LDAP path.

The userId must be a valid SAM account name, e.g. max. of 20 chars, letters and numerics only (except for a few valid special chars).

Are you complying with these requirements??

answered on Stack Overflow Sep 9, 2010 by marc_s

User contributions licensed under CC BY-SA 3.0