ADODB OleDbDataAdapter different count of rows from Active Directory

I

Ireneus Broncel

I have a class which reads Groups and Users from ActiveDirectory. The
Problem is, that i have about 10000 rows as product. When I am trying to
read the "memberOf" Objects out of this field i get allways different
count of rows.

If anybody knows something about this kind of problem, I would
appreciate any help.

Thx.

Here is the class:

------------------------------
using System;

using System.DirectoryServices;

using ActiveDs;
using System.Data;
using System.Globalization;

using System.Windows.Forms;
using System.Threading;

using ADODB;
using System.Data.OleDb;

using com.pironet.asp.rtool.database;

namespace com.pironet.asp.rtool.user
{
/// <summary>
/// Class for collecting informations
/// about user in ActiveDirectory
/// </summary>
public class AspGroupUserAD
{
private string dom = string.Empty;

DataTable adTable;
DataTable dbTable;

private string searchFilter = " WHERE objectClass = 'user' ";

/// <summary>
/// Empty
/// </summary>
public AspGroupUserAD()
{
Init();
}

private void Init()
{
DirectorySearcher DSESearcher = new DirectorySearcher();
string RootDSE = DSESearcher.SearchRoot.Path;

string ldapCompany= "OU=Hosting,";
this.dom = RootDSE.Insert(7, ldapCompany );

PopulateAdTable();
CreateDBUserTable();
FillDbDataTable();
}

public DataTable DataTable
{
get{ return dbTable ;}
set{ dbTable = value;}
}

private void WaitForAdTable(int count)
{
Thread.Sleep(count*120);
}

#region get all data of the user from active directory

/// <summary>
/// Rekursiver Durchlauf ohne Filter
/// </summary>
/// <param name="entry"></param>

public void PopulateAdTable()
{
string ldapUser= dom;

ADODB.Connection conn=new ADODB.Connection();
ADODB.Recordset rs=new ADODB.RecordsetClass();

conn.Provider="ADsDSOObject";
ADODB.Command cmd=new ADODB.Command();

conn.Open(conn.Provider,null,null,0);
cmd.ActiveConnection=conn;

OleDbDataAdapter daUsers=new OleDbDataAdapter();

string selectUser = "SELECT sn, objectGUID, msExchMailboxGuid,
lastLogon, userAccountControl, distinguishedName, memberOf FROM '" +
ldapUser + "'" + searchFilter +" ORDER BY sn ASC";

rs.Open( selectUser, conn, CursorTypeEnum.adOpenDynamic,
LockTypeEnum.adLockReadOnly, 1);

DataSet userDataSet = new DataSet("groupuser");

daUsers.Fill(userDataSet, rs,"groupuserAD");

this.adTable = userDataSet.Tables["groupuserAD"];

conn.Close();

}

#endregion



#region prepare for import in database


private void CreateDBUserTable()
{
this.dbTable = new DataTable("groupuserDB");

dbTable.Columns.Add("countID",System.Type.GetType("System.Int64"));
dbTable.Columns.Add("userGUID", System.Type.GetType("System.String"));
dbTable.Columns.Add("lastLogonTs", System.Type.GetType("System.Int64"));
dbTable.Columns.Add("groupGUID", System.Type.GetType("System.String"));
dbTable.Columns.Add("enabled", System.Type.GetType("System.String"));
dbTable.Columns.Add("recordDate",
System.Type.GetType("System.DateTime"));

//DataColumn [] primaryKeys = new DataColumn[2];
//primaryKeys[0] = dbTable.Columns["groupGUID"];
//primaryKeys[1] = dbTable.Columns["userGUID"];

//dbTable.PrimaryKey = primaryKeys;

}


private void FillDbDataTable()
{
Int64 countID = 0;

DateTime dateNow = DateTime.Now;

//MessageBox.Show( adTable.Rows.Count.ToString() );

WaitForAdTable( adTable.Rows.Count );


foreach( DataRow bufferRow in adTable.Rows )
{


DBNull dbNull = DBNull.Value;


if( bufferRow["memberOf"] != dbNull )
{

System.Object [] groupGUIDObject = ( System.Object [] )
bufferRow["memberOf"];

//string userGUID = this.GetUserGUID(bufferRow);
string userGUID = ConvertObjectGuidToString2(bufferRow);

Int64 lastLogonTs = this.GetLogonDateTs( bufferRow );
string enabled = this.IsAccountEnabled(bufferRow);

Thread.Sleep(10);

foreach( System.Object groupDN in groupGUIDObject )
{
try
{
DataRow topRow = this.dbTable.NewRow();

topRow["countID"] = countID;
topRow["userGUID"] = userGUID;
topRow["lastLogonTs"] = lastLogonTs;
topRow["enabled"] = enabled;
topRow["recordDate"] = dateNow;

topRow["groupGUID"] = this.GetGroupGUID((string) groupDN );

//topRow["groupGUID"] = GetGroupGUID2( (string) groupDN );

this.dbTable.Rows.Add(topRow);
countID++;
}
catch ( Exception ex )
{

}
}
}
}

}


private Int64 GetLogonDateTs( DataRow bufferRow )
{

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

return liTicks;
}

private string IsAccountEnabled( DataRow bufferRow )
{
int userAccountControl = (int) bufferRow["userAccountControl"];
int userAccountControl_Disabled = Convert.ToInt32(
ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE );

string enabled = "N";

int flagExists = userAccountControl & userAccountControl_Disabled;
//if a match is found, then the disabled flag exists within the
control flags

if(flagExists >0)
{
enabled = "N";
}
else
{
enabled = "Y";
}

return enabled;
}


public string GetUserGUID( DataRow bufferRow )
{

string scompany = bufferRow["distinguishedName"].ToString();

string sEntry = "LDAP://"+ scompany;
DirectoryEntry entry = new DirectoryEntry( sEntry, null, null,
AuthenticationTypes.Secure);

string nativeGUID = entry.NativeGuid;

string apiGUID = ConvertObjectGuidToString(bufferRow);
string handGUID = ConvertObjectGuidToString2(bufferRow);

MessageBox.Show( nativeGUID + "\n" + handGUID + "\n" + apiGUID);

//entry.Close();
//entry.Dispose();


return nativeGUID;
}

/// <summary>
/// Returns string array - first string in the array is customerGUID,
second customerID
/// </summary>
/// <param name="bufferRow"></param>
/// <returns></returns>

public string GetGroupGUID( string groupDN )
{
string groupGUID;
groupDN = groupDN.Trim();

try
{
string sEntry = "LDAP://"+ groupDN;
DirectoryEntry entry = new DirectoryEntry( sEntry, null, null,
AuthenticationTypes.FastBind);
groupGUID = entry.NativeGuid;

entry.Close();
entry.Dispose();
}
catch ( Exception ex )
{
throw new Exception(ex.Message, ex.InnerException );
}

return groupGUID;
}


// Get Guid by hand ;)
public string ConvertObjectGuidToString2(DataRow bufferRow)
{
Byte[] guid =(Byte[]) bufferRow["objectGUID"];

string buf = "";
string hexString;
int ubyteToInt;
for( int i = 0; i < guid.Length; i++)
{
ubyteToInt = ((int) guid) & 0x000000FF; //ubyte to int
hexString = ubyteToInt.ToString("x"); // To hexString
while( hexString.Length < 2) hexString = "0" + hexString;
buf = buf + hexString;
}
return buf.ToLower();
}


#endregion


}
}
---------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top