How to find a user on an LDAP server

When I try to find a user on an 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");

It seems that the following code fragment works (no exception is thrown), but the name samAccountName is encountered as an array of bytes. Does anyone know 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();
+3
source share
2 answers

The second code block works very well, however I did not pass the domain name in the DirectoryEntry initializer.

    Directory entry = new DirectoryEntry();
//other code
result.Properties["samAccountName"][0].ToString();
0
source

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

However: you do not tell us what you fill out for domain_namehere:

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

userId :

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

domain_name "" NetBIOS, . FABRIKAM - DNS, fabrikam.com AD-style, dc=fabrikom,dc=com LDAP.

userId SAM, . . 20 , ( ).

?

0

Source: https://habr.com/ru/post/1763979/


All Articles