gpt4 book ai didi

c# - System.DirectoryServices.Protocols Paged get all users 代码突然停止获取超过第一页的用户

转载 作者:太空狗 更新时间:2023-10-30 00:26:37 28 4
gpt4 key购买 nike

下面是使用 S.DS.P 的代码,一次可以非常快速地在 500 个页面中获取所有用户。

public List<AllAdStudentsCV> GetUsersDistinguishedNamePagedResults( string domain, string distinguishedName )
{
try
{
NetworkCredential credentials = new NetworkCredential( ConfigurationManager.AppSettings["AD_User"], ConfigurationManager.AppSettings["AD_Pass"] );
LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier( domain + ":389" );

List<AllAdStudentsCV> users = new List<AllAdStudentsCV>();

using (LdapConnection connection = new LdapConnection(directoryIdentifier, credentials))
{
string filter = "(&(objectClass=user)(objectCategory=person))";
string baseDN = ConfigurationManager.AppSettings["AD_DistinguishedName"];

string[] attribArray = {"name", "sAMAccountName", "objectGUID", "telexNumber", "HomePhone"};

List<SearchResultEntry> srList = PerformPagedSearch(connection, baseDN, filter, attribArray);

if (srList.Count == 0) return null;

foreach (SearchResultEntry entry in srList)
{
<...snip a bunch of code to filter out bad users by CN...>

users.Add( user );
}
catch ( Exception ex )
{
throw;
}
}
}
}
return users;
}
catch ( Exception ex )
{
throw;
}
}

private List<SearchResultEntry> PerformPagedSearch( LdapConnection connection, string baseDN, string filter, string[] attribs )
{
List<SearchResultEntry> results = new List<SearchResultEntry>();

SearchRequest request = new SearchRequest(
baseDN,
filter,
System.DirectoryServices.Protocols.SearchScope.Subtree,
attribs
);

PageResultRequestControl prc = new PageResultRequestControl(500);

//add the paging control
request.Controls.Add(prc);
int pages = 0;
while (true)
{
pages++;
SearchResponse response = connection.SendRequest(request) as SearchResponse;

//find the returned page response control
foreach (DirectoryControl control in response.Controls)
{
if (control is PageResultResponseControl)
{
//update the cookie for next set
prc.Cookie = ((PageResultResponseControl) control).Cookie;
break;
}
}

//add them to our collection
foreach (SearchResultEntry sre in response.Entries)
{
results.Add(sre);
}

//our exit condition is when our cookie is empty
if ( prc.Cookie.Length == 0 )
{
Trace.WriteLine( "Warning GetAllAdSdsp exiting in paged search wtih cookie = zero and page count =" + pages + " and user count = " + results.Count );
break;
}
}
return results;
}

它在 DEV 和 Prod 上完美运行,但在与 QA AD 服务器通信时突然停止在 QA 网络服务器上工作。它只返回一页然后停止。如果我将 DEV 指向 QA AD 服务器,它会正常工作...

它在 2012 年 2 月之前工作,上次我在 QA 中测试,到 2012 年 3 月 7 日肯定被破坏了

谁能想到会导致这种行为的任何事情?也许是 Windows 更新?我以前用过一个千斤顶这个产品......

我有理由相信这不是代码或配置问题...因为它适用于许多其他组合...它与 netowrk/securiyt/os 相关..但我无法弄清楚发生了什么变化。

欢迎任何帮助

最佳答案

有完全相同的问题,即在第一个页面之后没有返回任何页面。

这是我发现的解决问题的办法:

PageResultRequestControl pageRequestControl = new PageResultRequestControl(500);

SearchOptionsControl soc = new SearchOptionsControl(System.DirectoryServices.Protocols.SearchOption.DomainScope);

request.Controls.Add(pageRequestControl);
request.Controls.Add(soc);

不知道 SearchOptionsControl 的作用,但由于我添加了它,AD 返回了所有预期的对象。

关于c# - System.DirectoryServices.Protocols Paged get all users 代码突然停止获取超过第一页的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336553/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com