如何使用 C# 中的 LdapConnection 对象在 LDAP 中搜索用户?

P S*_*tha 4 c# ldap active-directory

我需要使用 C# 在 LDAP 目录中搜索用户。我可以使用DirectoryEntryand来完成此操作DirectorySearcher,如下面的代码所示:

SearchResultCollection sResults = null;

DirectoryEntry dEntry = new DirectoryEntry(_LDAPConnectionString);

DirectorySearcher dSearcher = new DirectorySearcher(dEntry);    
dSearcher.Filter = String.Format("(&(objectClass=user)(cn={0}))", userName);

sResults = dSearcher.FindAll();
Run Code Online (Sandbox Code Playgroud)

但要求是使用标准访问用户(始终相同)创建一个LdapConnection对象,如下所示。并使用该特定信息LdapConnectionObject来使用用户名搜索用户。

LdapConnection ldapConnectionObject = new LdapConnection(
                new LdapDirectoryIdentifier(_hostName, _port),
                null,
                AuthType.Basic);
ldapConnectionObject.Bind(accessUserCredential);
Run Code Online (Sandbox Code Playgroud)

如何使用上述内容ldapConnectionObject来搜索用户?

P S*_*tha 9

我找到了使用 LdapConnection 对象搜索的答案。因此,我们可以使用 LdapConnection 类的 SendRequest 方法,使用 SearchRequest 来获取搜索响应。在下面的示例中,我使用 uid userName 搜索了用户并检索了其 DN。

ldapConnection = new LdapConnection(
                new LdapDirectoryIdentifier(_hostName, _port),
                null,
                AuthType.Basic
                );

string searchFilter = String.Format("(&(objectClass=user)(uid={0}))", userName);

string userStore = "OU=WebsiteUsers,OU=InternalUsers";

SearchRequest searchRequest = new SearchRequest
                (userStore,
                 searchFilter,
                 System.DirectoryServices.Protocols.SearchScope.Subtree,
                 new string[] { "DistinguishedName" });

var response = (SearchResponse)ldapConnection.SendRequest(searchRequest);
string userDN = response.Entries[0].Attributes["DistinguishedName"][0].ToString();
Run Code Online (Sandbox Code Playgroud)

  • 但是参数`_userStore`。那是什么? (6认同)