TOPICS
Featured
Programming
Virtualization
OS
Microsoft
Databases
Our Experts Don’t Hallucinate
Get access to a community of Experts who can fact-check your AI code.
.NET Programming
137K
Questions
9K
Followers
Top Experts
I am getting an exception when I am running the following code. I would of thought
the limits I was setting would prevent it. So I am after any ways I can fix
the code to prevent the error from happening. To answer a question I will probably
get asked - yes I do need to do a large AD Search.
So any ideas?
Thanks,
Ward.
01// Here is the exception generated:
02//
03// System.DirectoryServices.DirectoryServicesCOMException (0x800700EA): More data is available.
04//
05// at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
06// at System.DirectoryServices.SearchResultCollection.get_InnerList()
07// at System.DirectoryServices.SearchResultCollection.get_Count()
08// at AD_Object_Discovery.Program.fnLDIFDE_Export_Containers(String starting_path)
09//
10
11
12namespace AD_Object_Discovery
13{
14
15
16 partial class Program
17 {
18 // -------------------------------------------------------------------
19 // fnSetSearcher_Settings: Set the searcher settings.
20 // -------------------------------------------------------------------
21
22 public static void fnSetSearcher_Settings(ref DirectorySearcher searcher)
23 {
24 searcher.PageSize = 1000;
25 searcher.SizeLimit = 10000000;
26 }
27
28
29 public function fnLogException(Exception ex)
30 {
31 Console.WriteLine(ex.ToString());
32 }
33
34 // -------------------------------------------------------------------
35 // fnLDIFDE_Export_Containers: Export all containers to a file.
36 // -------------------------------------------------------------------
37
38 static void fnLDIFDE_Export_Containers(string starting_path)
39 {
40 try
41 {
42
43 fnWrite_Section("Active Directory Containers (objectClass=container)");
44
45 DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
46 string filter = "(objectClass=container)";
47
48 DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);
49
50 fnSetSearcher_Settings(ref searcher);
51
52 searcher.PropertiesToLoad.Add("cn");
53 searcher.PropertiesToLoad.Add("description");
54 searcher.PropertiesToLoad.Add("distinguishedName");
55 searcher.PropertiesToLoad.Add("name");
56 searcher.PropertiesToLoad.Add("objectGUID");
57 searcher.PropertiesToLoad.Add("showInAdvancedViewOnly");
58
59
60 SearchResultCollection ad_results = searcher.FindAll();
61
62 foreach (SearchResult result in ad_results)
63 {
64 }
65
66 ad_results.Dispose();
67 searcher.Dispose();
68 BaseOU.Dispose();
69
70 }
71
72 catch (Exception ex)
73 {
74 fnLogException(ex);
75 }
76 }
77 }
78
79}
80
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Thanks for the response - can you help me out with the initilaization code.
I would like to be able to logon with the current users credentials and connect to a LDAP path.
I have posted a test program based on what you sent so you can tell me where I am going wrong.
Thanks.
Ward.
001using System;
002using System.Collections.Generic;
003using System.Linq;
004using System.Text;
005using System.DirectoryServices;
006using System.DirectoryServices.Protocols;
007using System.Net;
008
009// Paging in System.DirectoryServices.Protocols
010// http://dunnry.com/blog/PagingInSystemDirectoryServicesProtocols.aspx
011
012
013namespace SDSPTest
014{
015 class Program
016 {
017 public static List<SearchResultEntry> fnPerformPagedSearch(
018LdapConnection connection,
019string baseDN,
020string filter,
021string[] attribs)
022 {
023
024 List<SearchResultEntry> results = new List<SearchResultEntry>();
025
026 SearchRequest request = new SearchRequest(
027 baseDN,
028 filter,
029 System.DirectoryServices.Protocols.SearchScope.Subtree,
030 attribs
031 );
032
033 PageResultRequestControl prc = new PageResultRequestControl(500);
034
035 //add the paging control
036
037 request.Controls.Add(prc);
038
039 while (true)
040 {
041 SearchResponse response = connection.SendRequest(request) as SearchResponse;
042 //find the returned page response control
043 foreach (DirectoryControl control in response.Controls)
044 {
045
046 if (control is PageResultResponseControl)
047 {
048 //update the cookie for next set
049
050 prc.Cookie = ((PageResultResponseControl)control).Cookie;
051 break;
052 }
053 }
054
055
056 //add them to our collection
057
058 foreach (SearchResultEntry sre in response.Entries)
059 {
060 results.Add(sre);
061 }
062
063 //our exit condition is when our cookie is empty
064
065 if (prc.Cookie.Length == 0)
066 break;
067
068 }
069
070 return results;
071 }
072
073
074 static void Main(string[] args)
075 {
076 string baseDN = "LDAP://DC=acme,DC=net";
077 List<SearchResultEntry> ad_results = new List<SearchResultEntry>();
078 LdapConnection connection = new LdapConnection(baseDN);
079 // NetworkCredential credential = new NetworkCredential("user1", "password1", "fabrikam");
080 NetworkCredential credential = new NetworkCredential("", "", "");
081
082 string filter = "(objectClass=organizationalUnit)";
083
084 string[] ad_attribs = new string[9];
085 ad_attribs[0] = "name";
086 ad_attribs[1] = "description";
087 ad_attribs[2] = "objectGUID";
088 ad_attribs[3] = "postalCode";
089 ad_attribs[4] = "street";
090 ad_attribs[5] = "st";
091 ad_attribs[6] = "l";
092 ad_attribs[7] = "c";
093 ad_attribs[8] = "distinguishedName";
094
095
096 ad_results = fnPerformPagedSearch(connection, baseDN, filter, ad_attribs);
097 }
098 }
099}
100
Hi thanks for your response - I think I am missing it but how it different that you posted different from the
original method / code I am using.
Thanks,
Ward.
EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
.NET Programming
137K
Questions
9K
Followers
Top Experts
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
REWARDS PROGRAM
Earn free swag for participating on the platform. Ask your first question and earn a free t-shirt.