什么是在.NET中进行Active Directory搜索分页的有效方法?在AD中搜索的方法有很多,但到目前为止我找不到如何有效地进行搜索.我希望能够指示Skip和Take参数,并能够检索与结果中的搜索条件匹配的记录总数.
我试过在PrincipalSearcher课堂上搜索:
using (var ctx = new PrincipalContext(ContextType.Domain, "FABRIKAM", "DC=fabrikam,DC=com"))
using (var criteria = new UserPrincipal(ctx))
{
criteria.SamAccountName = "*foo*";
using (var searcher = new PrincipalSearcher(criteria))
{
((DirectorySearcher)searcher.GetUnderlyingSearcher()).SizeLimit = 3;
var results = searcher.FindAll();
foreach (var found in results)
{
Console.WriteLine(found.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我能够将搜索结果限制为3但是我无法获得与我的搜索条件(SamAccountName包含foo)相对应的记录总数.我也没有能够向搜索者指示跳过前50个记录.
我也试过使用System.DirectoryServices.DirectoryEntry,System.DirectoryServices.Protocols.SearchRequest但我唯一能做的就是指定页面大小.
那么获取客户端上的所有结果并在那里进行跳过和计数的唯一方法是什么?我真的希望有更有效的方法直接在域控制器上实现这一点.
我正在使用ADS Directory搜索器findAll()方法搜索现有登录(如下面的代码所示).似乎findall方法只返回1000个条目,尽管有更多的条目.如何查找每次登录的所有()?
IList<string> adslist = new List<string>();
using (DirectoryEntry de = new DirectoryEntry("LDAP://armlink.com", null, null, AuthenticationTypes.Secure))
using (DirectorySearcher ds = new DirectorySearcher(de, "(objectclass=user)", new string[] { "samaccountname" }))
foreach (SearchResult sr in ds.FindAll())
{
string[] e = sr.Path.Split(new string[] { "LDAP://", "OU=", ",", "DC=", ".com", "/CN=" }, StringSplitOptions.RemoveEmptyEntries);
ResultPropertyCollection pc = sr.Properties;
adslist.Add(e[0] + "/" + pc["samaccountname"][0].ToString());
// Debug.WriteLine(adslist.Last());
}
Run Code Online (Sandbox Code Playgroud) using (DirectorySearcher srch = new DirectorySearcher(String.Format("(memberOf= {0})",p_Target.DistinguishedName)))
{
srch.PageSize = 2;
SearchResultCollection results = results = srch.FindAll();
int count = results.Count;
}
Run Code Online (Sandbox Code Playgroud)
count = 3(THREE)而不是2.为什么?我不希望只在一个页面中获得所有结果.我知道PageSize = 2很小,但我在这种情况下设置的值只是为了测试目的(实际上它会更多).
我正在尝试在iPlanet LDAP上进行分页搜索.这是我的代码:
LdapConnection ldap = new LdapConnection("foo.bar.com:389");
ldap.AuthType = AuthType.Anonymous;
ldap.SessionOptions.ProtocolVersion = 3;
PageResultRequestControl prc = new PageResultRequestControl(1000);
string[] param = new string[] { "givenName" };
SearchRequest req = new SearchRequest("ou=people,dc=bar,dc=com", "(ou=MyDivision)", SearchScope.Subtree, param);
req.Controls.Add(prc);
while (true)
{
SearchResponse sr = (SearchResponse)ldap.SendRequest(req);
... snip ...
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到一个异常,指出"服务器不支持控件.控件是关键的"在剪辑之前的行上.快速谷歌搜索没有任何结果.iPlanet是否支持分页?如果是这样,我做错了什么?谢谢.