我们正在使用System.DirectoryServices.DirectorySearcher进行sAMAccountName查找。这很好用,除了当查询某个广告时,我们怀疑它相当大,搜索经常会超时。在做了一些研究后,我发现在查询大型广告时,使用System.DirectoryServices.Protocols可以更快地进行搜索。我正在尝试使用协议重新创建我们拥有的内容,看看这是否会对超时产生任何影响。这就是目前的情况:
Dim Entry As New DirectoryEntry(anLDAPURL, aDomainUserName, aPassword)
Dim obj As Object = Entry.NativeObject 'Force Authentication on Active Directory Server
Dim Filter As String = String.Format("(sAMAccountName={0})", aDomainUserName)
Dim Search As New DirectorySearcher(Entry, Filter)
Search.PropertiesToLoad.Add(SID)
Search.PropertiesToLoad.Add(ACCOUNTISLOCKEDOUT)
Search.PropertiesToLoad.Add(ACCOUNTISDISABLED)
Dim Results As SearchResult = Search.FindOne()这工作得很好,而且速度非常快(除了上面提到的超时的情况)。这就是我想要改变的,这样我就可以测试它了:
Dim credentials As New System.Net.NetworkCredential(aDomainUserName, aPassword)
Dim directoryIdentifier As New System.DirectoryServices.Protocols.LdapDirectoryIdentifier("ldap-ad.example.org")
Using connection As New System.DirectoryServices.Protocols.LdapConnection(directoryIdentifier, credentials, Protocols.AuthType.Basic)
Dim attributes() As String = {SID, ACCOUNTISLOCKEDOUT, ACCOUNTISDISABLED}
Dim search As New System.DirectoryServices.Protocols.SearchRequest(
"dc=example,dc=org",
String.Format("(sAMAccountName={0})", aDomainUserName),
Protocols.SearchScope.Subtree,
attributes)
Dim response As System.DirectoryServices.Protocols.SearchResponse = DirectCast(connection.SendRequest(search), System.DirectoryServices.Protocols.SearchResponse)
End Using上面的代码可以工作,因为它返回一个结果,但比原始代码慢得多。我怀疑我试图查询的方式是低效的,但我不太确定我应该如何设置它,以便它更快。
发布于 2014-03-20 05:53:09
我遇到了同样的问题,最终是由于System.DirectoryServices.Protocols.LdapConnection.SendRequest方法中返回结果中的“引用追逐”。这是由于没有任何DNS条目的“假”域名"corp.org“造成的(所以SendRequest在结果上做DNS查询浪费了很多时间)。要禁用推荐追踪,请执行以下操作:
var conn = new LdapConnection(...);
conn.SessionOptions.ReferralChasing = ReferralChasingOptions.None;发布于 2013-06-13 11:42:54
根据您在SearchRequest构造函数("dc=example,dc=org")中的LDAP路径,看起来您是在LdapDirectoryIdentifier构造函数(ldap-ad.example.org)中指定了一个服务器。您是否尝试过仅指定域而不是服务器(example.org)?
当你在一个只返回0或1结果的索引属性上进行搜索时,我真的看不出这两个方法有什么不同。
https://stackoverflow.com/questions/16890091
复制相似问题