tags:

views:

81

answers:

2

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();
A: 

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();
RandomNoob
A: 

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??

marc_s
Ok, I've updated the code to reflect the variables I'm using...
@bastos.sergio: as I expected - you're not using a proper NetBIOS style domain name - you should be having a domain name such as `GANDALF` or something like that - just a single word, no dots in it - the "old-style" domain name from pre-Active Directory times.
marc_s
If I put GANDALF I get the same error as above. If I put INTRAFG I get the error "The server could not be contacted"
@bastos.sergio: what if you just use `PrincipalContext domainContext = new PrincipalContext(ContextType.Domain)` - don't specify any domain name. I believe this will pick the current domain your machine is in.
marc_s
But my machine isn't in the Domain, doing just that I get the error "The server could not be contacted"
@bastos.sergio: well if your machine isn't in the domain..... then you cannot create the domain context without specifying a domain user and password ! Try to find an overload where you can define a (domain) username and password.
marc_s