System.DirectoryServices.Protocols Paged让所有用户代码突然停止获得超过第一页用户

Eri*_*Cal 4 c# directoryservices ldap active-directory

所以这里是使用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;
        }
Run Code Online (Sandbox Code Playgroud)

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

它在2012年2月之前工作,上次我在质量保证方面进行了测试,并且在2012年3月7日之前确实已经破了

谁能想到会导致这种行为的任何事情?也许是Windows更新?我之前有一个杰克这个产品......

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

任何帮助都是适用的

小智 8

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

以下是我发现解决问题的方法:

PageResultRequestControl pageRequestControl = new PageResultRequestControl(500);

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

request.Controls.Add(pageRequestControl);
request.Controls.Add(soc);
Run Code Online (Sandbox Code Playgroud)

不知道SearchOptionsControl做了什么,但是自从我添加了它之后,AD返回所有预期的对象.