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

时间:2012-04-26 15:27:21

标签: c# active-directory ldap directoryservices

所以这里是使用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月之前工作,上次我在质量保证方面进行了测试,并且在2012年3月7日之前肯定已经破解了

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

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

任何帮助都是适用的

2 个答案:

答案 0 :(得分:8)

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

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

PageResultRequestControl pageRequestControl = new PageResultRequestControl(500);

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

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

不知道SearchOptionsControl做了什么,但是自从我添加了这个,AD返回了所有预期的对象。

答案 1 :(得分:1)

此行解决了该问题(连接为LdapConnection)->

connection.SessionOptions.ReferralChasing = ReferralChasingOptions.None;

https://social.msdn.microsoft.com/Forums/vstudio/en-US/17957bb2-15b4-4d44-80fa-9b27eb6cb61f/pageresultrequestcontrol-cookie-always-zero?forum=csharpgeneral