whorsfall
asked on
Active Directory Searching
Hi,
I am using two different methods to search active directory and I notice that the first method fnADSearch1() does not return a full result set. I seems to return a number between 600 and 800 Ou's. Where as using fnADSearch2() always returns the correct 3550. So, I would like to find out how to adjust the code of fnADSearch1() so I return a full result set each time.
Thanks,
Ward
I am using two different methods to search active directory and I notice that the first method fnADSearch1() does not return a full result set. I seems to return a number between 600 and 800 Ou's. Where as using fnADSearch2() always returns the correct 3550. So, I would like to find out how to adjust the code of fnADSearch1() so I return a full result set each time.
Thanks,
Ward
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Net;
// Paging in System.DirectoryServices.Protocols
// http://dunnry.com/blog/PagingInSystemDirectoryServicesProtocols.aspx
namespace ADTest
{
class Program
{
public static List<SearchResultEntry> fnPerformPagedSearch(
LdapConnection connection,
string baseDN,
string filter,
string[] attribs)
{
List<SearchResultEntry> results = new List<SearchResultEntry>();
PageResultRequestControl prc = null;
SearchRequest request = null;
try
{
request = new SearchRequest(
baseDN,
filter,
System.DirectoryServices.Protocols.SearchScope.Subtree,
attribs
);
request.TimeLimit = new TimeSpan(2, 0, 0);
request.SizeLimit = 1000000;
}
catch (Exception ex)
{
Console.WriteLine("Building SeaechRequest");
Console.WriteLine(ex.ToString());
}
try
{
prc = new PageResultRequestControl(1000);
}
catch (Exception ex)
{
Console.WriteLine("Creating prc");
Console.WriteLine(ex.ToString());
}
//add the paging control
request.Controls.Add(prc);
try
{
while (true)
{
TimeSpan timeout = new TimeSpan(2, 0, 0);
SearchResponse response = connection.SendRequest(request, timeout) 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)
break;
}
return results;
}
catch (LdapException ldapex)
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Exception: \n");
Console.WriteLine(ldapex.ToString());
Console.ResetColor();
return results;
}
catch (Exception ex)
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Exception: \n");
Console.WriteLine(ex.ToString());
Console.ResetColor();
return results;
}
}
static void fnADSearch1()
{
DateTime start = DateTime.Now;
Console.WriteLine("ADSearch1");
string connection_path = "acme.net";
string baseDN = "DC=acme,DC=net";
List<SearchResultEntry> ad_results = new List<SearchResultEntry>();
LdapConnection connection = new LdapConnection(connection_path);
connection.Timeout = new TimeSpan(2, 0, 0);
string filter = "(objectClass=organizationalUnit)";
string[] ad_attribs = new string[9];
ad_attribs[0] = "name";
ad_attribs[1] = "description";
ad_attribs[2] = "objectGUID";
ad_attribs[3] = "postalCode";
ad_attribs[4] = "street";
ad_attribs[5] = "st";
ad_attribs[6] = "l";
ad_attribs[7] = "c";
ad_attribs[8] = "distinguishedName";
Console.WriteLine("");
ad_results = fnPerformPagedSearch(connection, baseDN, filter, ad_attribs);
DateTime end = DateTime.Now;
if (ad_results.Count < 1)
{
Environment.Exit(1);
}
Console.WriteLine();
Console.WriteLine("ADSearch1: OUs: {0}", ad_results.Count);
TimeSpan duration = end - start;
Console.WriteLine("");
Console.WriteLine("ADSearch1 Time {0}:{1}:{2}\n", duration.Hours, duration.Minutes, duration.Seconds);
}
static void fnADSearch2()
{
try
{
DateTime start = DateTime.Now;
Console.WriteLine("ADSearch2");
string starting_path = "LDAP://DC=acme,DC=net";
DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
string filter = "(objectClass=organizationalUnit)";
DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);
searcher.PageSize = 1000;
searcher.SizeLimit = 10000000;
searcher.ServerTimeLimit = new TimeSpan(2, 0, 0);
searcher.ClientTimeout = new TimeSpan(2, 0, 0);
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add("description");
searcher.PropertiesToLoad.Add("objectGUID");
searcher.PropertiesToLoad.Add("postalCode");
searcher.PropertiesToLoad.Add("street");
searcher.PropertiesToLoad.Add("st");
searcher.PropertiesToLoad.Add("l");
searcher.PropertiesToLoad.Add("c");
searcher.PropertiesToLoad.Add("distinguishedName");
// public string objectGUID = null;
SearchResultCollection ad_results = searcher.FindAll();
DateTime end = DateTime.Now;
Console.WriteLine("ADSearch2: OUs {0}", ad_results.Count);
TimeSpan duration = end - start;
Console.WriteLine("");
Console.WriteLine("ADSearch2 Time {0}:{1}:{2}", duration.Hours, duration.Minutes, duration.Seconds);
}
catch
{
}
}
static void Main(string[] args)
{
Console.WriteLine("");
fnADSearch1();
fnADSearch2();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.