public IEnumerable <User> GetUsers(string searchFilter) { _connection.Connect(_config.Url, _config.Port); _connection.Bind(_config.BindDn, _config.BindCredentials); int startIndex = 1; int contentCount = 0; int afterIndex = 1000; int count = 0; do { LdapVirtualListControl ctrl = new LdapVirtualListControl(startIndex, 0, afterIndex, contentCount); LdapSortKey[] keys = new LdapSortKey[1]; keys[0] = new LdapSortKey("samaccountname"); LdapSortControl sort = new LdapSortControl(keys, true); LdapSearchConstraints constraints = _connection.SearchConstraints; constraints.setControls(new LdapControl[] { ctrl, sort }); _connection.Constraints = constraints; LdapSearchResults lsc = _connection.Search( _config.SearchBase, LdapConnection.SCOPE_SUB, searchFilter, new[] { MemberOfAttribute, DisplayNameAttribute, SamAccountNameAttribute, "sn", "givenName", "mail", "displayName" }, false, constraints); foreach (var item in lsc.ToList()) { var user = new User { DisplayName = $"{item.getAttribute("displayName")?.StringValue ?? "noSn"}", Sam = item.getAttribute(SamAccountNameAttribute)?.StringValue ?? "noSam", IsAdmin = item.getAttribute(MemberOfAttribute)?.StringValueArray.Contains(_config.AdminCn) ?? false, Subordinates = null, Email = item.getAttribute("mail")?.StringValue ?? "noMail" }; yield return(user); } LdapControl[] controls = lsc.ResponseControls; if (controls == null) { Console.Out.WriteLine("No controls returned"); } else { foreach (LdapControl control in controls) { if (control.ID == "2.16.840.1.113730.3.4.10") { LdapVirtualListResponse response = new LdapVirtualListResponse(control.ID, control.Critical, control.getValue()); startIndex += afterIndex + 1; contentCount = response.ContentCount; count += afterIndex; } } } // Console.WriteLine(i); } while (count <= contentCount); yield break; }