473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Slow ActiveDirectory responses

Hi all,

I have a Problem with this code, I am using Threads in the main form but
to load all the users from one OU (there are about 700 of them, not
realy very much) it needs about 10-20 sek ( without Threads in the main
form even 1 min).

Do you know how to speed up the searching process?
Thx
Susan

Code: Ho do I get the DataTable full faster?

-------------snip-------------
using System;
using System.DirectoryServices;

using ActiveDs;
using System.Data;

using System.Windows.Forms;

namespace com.pironet.asp.user
{
/// <summary>
///
/// </summary>
public class UserDataSet
{
private static DataSet userDataSet;
private static DataTable userDataTable;

//Root to search from.
private static string dom = ADHelper.GetLDAPDomain();

private static int counter = 0;

static string searchFilter =
("(&(objectCategory=Person)(objectClass=user))" );

/// <summary>
/// Empty
/// </summary>
public UserDataSet()
{

}

/// <summary>
/// Creates the Table and fills the Objects
/// </summary>
static UserDataSet()
{
counter = 0;

userDataTable = new DataTable("users");
userDataSet = new DataSet("users");

CreateTable();
PopulateTable();
}

public static DataSet DataSet
{
get{ return userDataSet ;}
set{ userDataSet = value;}
}

public static DataTable DataTable
{
get{ return userDataTable ;}
set{ userDataTable = value;}
}


private static void CreateTable()
{
//Create Columns for DataTable.
userDataTable.Columns.Add("Company",
System.Type.GetType("System.String"));
userDataTable.Columns.Add("Office",
System.Type.GetType("System.String"));

userDataTable.Columns.Add("Vorname",System.Type.Ge tType("System.String"));

userDataTable.Columns.Add("Nachname",System.Type.G etType("System.String"));
userDataTable.Columns.Add("Name",System.Type.GetTy pe("System.String"));

userDataTable.Columns.Add("LastLogin",System.Type. GetType("System.String"));
userDataTable.Columns.Add("CN",System.Type.GetType ("System.String"));

userDataTable.Columns.Add("LastLoginTs",System.Typ e.GetType("System.String"));
}



private static void PopulateTable()
{

DirectoryEntry entry = new DirectoryEntry( dom, null, null,
AuthenticationTypes.Secure);
CManagerMain.cManagerMain.progressBar1.Value = 1;
GetChildren2(entry);
userDataSet.Tables.Add(userDataTable);
CManagerMain.cManagerMain.progressBar1.Value = 1;

//MessageBox.Show("Treffer * :" + counter);
}


private static void GetChildren2(DirectoryEntry dirEntry)
{

DirectorySearcher src = new DirectorySearcher(dirEntry);

//src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name =" +
sRegEx + "))");
//src.Filter= ("(&(objectCategory=Person)(objectClass=user))" );

src.Filter= searchFilter;
src.SearchRoot = dirEntry;

foreach( SearchResult res in src.FindAll() )
{
DirectoryEntry child= res.GetDirectoryEntry();

DataRow topRow = userDataTable.NewRow();

topRow = FillDataRow( topRow, child.Properties["company"], "Company" );
topRow = FillDataRow( topRow,
child.Properties["physicalDeliveryOfficeName"], "Office" );
topRow = FillDataRow( topRow, child.Properties["givenName"],
"Vorname" );
topRow = FillDataRow( topRow, child.Properties["sn"], "Nachname" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLogin" );
topRow = FillDataRow( topRow, child.Properties["sAMAccountName"],
"Name" );

topRow = FillDataRow( topRow, child.Properties["distinguishedName"],
"CN" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLoginTs" );

userDataTable.Rows.Add(topRow);

CManagerMain.cManagerMain.progressBar1.PerformStep ();
//counter++;
}
}


public static void SetSearchFilter( String category, String regEx)
{
if( category == null || regEx == null)
{
searchFilter = ("(&(objectCategory=Person)(objectClass=user))" );
} else {
searchFilter = ("(&(objectCategory=Person)(objectClass=user)(" +
category + "=" + regEx + "))");
}
}
private static bool Search( DirectoryEntry dirEntry, String category,
String sRegEx )
{
bool treffer = false;

DirectorySearcher src = new DirectorySearcher(dirEntry);
//string sRegEx = "10020047*";

src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name =" +
sRegEx + "))");
src.SearchRoot = dirEntry;

int count = src.FindAll().Count;

if(count > 0) treffer=true;
return treffer;
}

private static DataRow FillDataRow( DataRow dataRow,
PropertyValueCollection property , string columnName )
{

if( property.Value != null )
{
if( columnName.Equals("LastLogin") )
{
dataRow[columnName] = GetDate(property);
}
else if( columnName.Equals("LastLoginTs") )
{
dataRow[columnName] = GetDateTs(property);
}
else
{
dataRow[columnName] = property[0].ToString();
}
}
else
{
dataRow[columnName] = "";
}
return dataRow;
}

private static string GetDate( PropertyValueCollection property )
{
string date = "no login yet";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference
to ILargeInteger
Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;

if( liTicks > 0 && DateTime.MaxValue.Ticks >= liTicks &&
DateTime.MinValue.Ticks <= liTicks)
{
DateTime dTemp = DateTime.FromFileTime(liTicks);
date = dTemp.ToShortDateString()+ " " + dTemp.ToShortTimeString();
}

return date;
}
private static string GetDateTs( PropertyValueCollection property )
{
string ts = "0";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference
to ILargeInteger

Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;
ts = liTicks.ToString();

return ts;
}

}
}
-------------snap--------------

Nov 16 '05 #1
1 6345
You have to find out how much time it takes to fill the
PropertyValueCollection (see src.FindAll() ).
If this take 10-20 seconds there is only one thing you can do in your code
to speed up the process, that is restrict the properties transferred to
those you really need by setting the PropertiesToLoad like this:

string[] props = {"lastLogon", "physicalDeliveryOfficeName", "givenName",
"sn", "sAMAccountName", "distinguishedname", "company"};
src.PropertiesToLoad.AddRange(props);

, if that doesn't change much, you will need to check your network
connection and/or AD server load to find the bottleneck.

However, when FindAll() takes less than a few seconds, you need post a
complete (short) working repro.

Willy.

"Susanne Christe" <at**@gmx.net> wrote in message
news:40********@news.pironet-ndh.com...
Hi all,

I have a Problem with this code, I am using Threads in the main form but
to load all the users from one OU (there are about 700 of them, not realy
very much) it needs about 10-20 sek ( without Threads in the main form
even 1 min).

Do you know how to speed up the searching process?
Thx
Susan

Code: Ho do I get the DataTable full faster?

-------------snip-------------
using System;
using System.DirectoryServices;

using ActiveDs;
using System.Data;

using System.Windows.Forms;

namespace com.pironet.asp.user
{
/// <summary>
///
/// </summary>
public class UserDataSet
{
private static DataSet userDataSet;
private static DataTable userDataTable;

//Root to search from.
private static string dom = ADHelper.GetLDAPDomain();

private static int counter = 0;

static string searchFilter =
("(&(objectCategory=Person)(objectClass=user))" );

/// <summary>
/// Empty
/// </summary>
public UserDataSet()
{

}

/// <summary>
/// Creates the Table and fills the Objects
/// </summary>
static UserDataSet()
{
counter = 0;

userDataTable = new DataTable("users");
userDataSet = new DataSet("users");

CreateTable();
PopulateTable();
}

public static DataSet DataSet
{
get{ return userDataSet ;}
set{ userDataSet = value;}
}

public static DataTable DataTable
{
get{ return userDataTable ;}
set{ userDataTable = value;}
}


private static void CreateTable()
{
//Create Columns for DataTable.
userDataTable.Columns.Add("Company",
System.Type.GetType("System.String"));
userDataTable.Columns.Add("Office", System.Type.GetType("System.String"));

userDataTable.Columns.Add("Vorname",System.Type.Ge tType("System.String"));

userDataTable.Columns.Add("Nachname",System.Type.G etType("System.String"));
userDataTable.Columns.Add("Name",System.Type.GetTy pe("System.String"));

userDataTable.Columns.Add("LastLogin",System.Type. GetType("System.String"));
userDataTable.Columns.Add("CN",System.Type.GetType ("System.String"));

userDataTable.Columns.Add("LastLoginTs",System.Typ e.GetType("System.String"));
}



private static void PopulateTable()
{
DirectoryEntry entry = new DirectoryEntry( dom, null, null,
AuthenticationTypes.Secure);
CManagerMain.cManagerMain.progressBar1.Value = 1;
GetChildren2(entry);
userDataSet.Tables.Add(userDataTable);
CManagerMain.cManagerMain.progressBar1.Value = 1;

//MessageBox.Show("Treffer * :" + counter);
}


private static void GetChildren2(DirectoryEntry dirEntry)
{

DirectorySearcher src = new DirectorySearcher(dirEntry);
//src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name =" +
sRegEx + "))");
//src.Filter= ("(&(objectCategory=Person)(objectClass=user))" );

src.Filter= searchFilter;
src.SearchRoot = dirEntry;

foreach( SearchResult res in src.FindAll() )
{
DirectoryEntry child= res.GetDirectoryEntry();

DataRow topRow = userDataTable.NewRow();

topRow = FillDataRow( topRow, child.Properties["company"], "Company" );
topRow = FillDataRow( topRow,
child.Properties["physicalDeliveryOfficeName"], "Office" );
topRow = FillDataRow( topRow, child.Properties["givenName"], "Vorname" );
topRow = FillDataRow( topRow, child.Properties["sn"], "Nachname" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLogin" );
topRow = FillDataRow( topRow, child.Properties["sAMAccountName"],
"Name" );

topRow = FillDataRow( topRow, child.Properties["distinguishedName"],
"CN" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLoginTs" );

userDataTable.Rows.Add(topRow);

CManagerMain.cManagerMain.progressBar1.PerformStep ();
//counter++;
}
}


public static void SetSearchFilter( String category, String regEx)
{
if( category == null || regEx == null)
{
searchFilter = ("(&(objectCategory=Person)(objectClass=user))" );
} else {
searchFilter = ("(&(objectCategory=Person)(objectClass=user)(" + category
+ "=" + regEx + "))");
}
}
private static bool Search( DirectoryEntry dirEntry, String category,
String sRegEx )
{
bool treffer = false;

DirectorySearcher src = new DirectorySearcher(dirEntry); //string sRegEx =
"10020047*";

src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name =" + sRegEx
+ "))");
src.SearchRoot = dirEntry;

int count = src.FindAll().Count;

if(count > 0) treffer=true;
return treffer;
}

private static DataRow FillDataRow( DataRow dataRow,
PropertyValueCollection property , string columnName )
{

if( property.Value != null )
{
if( columnName.Equals("LastLogin") )
{
dataRow[columnName] = GetDate(property);
}
else if( columnName.Equals("LastLoginTs") )
{
dataRow[columnName] = GetDateTs(property);
}
else
{
dataRow[columnName] = property[0].ToString();
}
}
else
{
dataRow[columnName] = "";
}
return dataRow;
}

private static string GetDate( PropertyValueCollection property )
{
string date = "no login yet";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference to
ILargeInteger
Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;

if( liTicks > 0 && DateTime.MaxValue.Ticks >= liTicks &&
DateTime.MinValue.Ticks <= liTicks)
{
DateTime dTemp = DateTime.FromFileTime(liTicks);
date = dTemp.ToShortDateString()+ " " + dTemp.ToShortTimeString();
}

return date;
}
private static string GetDateTs( PropertyValueCollection property )
{
string ts = "0";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference to
ILargeInteger

Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;
ts = liTicks.ToString();

return ts;
}

}
}
-------------snap--------------

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dirk Hagemann | last post by:
Hi! I'm working in an ActiveDirectory Network and want to get some information about a user-account or computer (OS for example) account. I think this works with the win32com.client module and the...
2
by: bob | last post by:
Hi, Is this the right place to dicuss Visual Studio 2005 and .Net 2? I am try to implement a class (already using Windows Authentication) where I am given a user name and I return a list of...
7
by: ddsvi78 | last post by:
I am a complete idiot when it comes to access. Now that said, I work for a computer security company and one of our customers came to us with an access problem. They had been running fine for a...
4
by: Sargas Atum | last post by:
Hi all, I am trying to get the field "lastLogon" out of ActiveDirectory. It should be trivial I thought, but it is not, as I have experienced. private DataRow FillDataRow( DataRow dataRow, ...
4
by: ruca | last post by:
Hi, I have a Login Page that have Active Directory permissions. I can get the domain and the user that are trying to access application, but I need to "know" the password that user insert,...
12
by: Ian Murphy | last post by:
Hopefully someone else has seen something similar. We are experiencing an odd problem with aspnet, iis6 and w2k3. I have written a small site which works fine during the day. Each morning however...
0
by: dhnriverside | last post by:
Hi guys I want to find out which of my ActiveDirectory users is logged on to a particular ActiveDirectory Computer. I can get what Computer a user is logged on to with ...
0
by: killbill | last post by:
Hi All, I want to retrieve certificates assigned to a specific ActiveDirectory User. How i can do that using DirectoryServices namespace or is there any other way. I have done other user...
0
by: richardaz | last post by:
I have been working on a project to search for 250,000 different records in ActiveDirectory. The process takes a long time and I have tried everything to make it faster. Currently using...
0
by: myth0s | last post by:
Hi, The question: Is it possible to have two differents ActiveDirectory Membership Provider in web.config and change at run-time from the default provider to the second provider if the first one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.