4

所以这里是使用 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 相关......但我无法弄清楚发生了什么变化。

任何帮助都可以使用

4

2 回答 2

9

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

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

PageResultRequestControl pageRequestControl = new PageResultRequestControl(500);

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

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

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

于 2012-05-23T14:23:52.473 回答
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

于 2019-02-07T08:53:10.207 回答