473,320 Members | 1,856 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,320 software developers and data experts.

ADODB OleDbDataAdapter different count of rows from Active Directory

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[i]) & 0x000000FF; //ubyte to int
hexString = ubyteToInt.ToString("x"); // To hexString
while( hexString.Length < 2) hexString = "0" + hexString;
buf = buf + hexString;
}
return buf.ToLower();
}
#endregion
}
}
---------------------------------------------------------------
Nov 16 '05 #1
0 4643

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

Similar topics

0
by: Skip Montanaro | last post by:
Consider the output of these two explain statements: mysql> explain select count(*) from cities,addresses,venues,events where cities.latitude <= 30.2741903768 and cities.latitude >=...
0
by: elcc1958 | last post by:
I need to support a VB6 application that will be receiving disconnected ADODB.Recordset from out DotNet solution. Our dotnet solution deals with System.Data.DataTable. I need to populate a...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
6
by: Marcel Hug | last post by:
Hi all ! I have a table in my database, which has 3 attributes. IDFailureControl, ControlDate and ControlVersion. In the following function I test, if the date of today allready exists. Then I...
3
by: Yuk Tang | last post by:
I'm trying to grab the fieldnames and values from a recordset, but I'm getting errors. I have an idea what the error might come from, but I'm not sure how to correct it. I'm connecting to an...
4
by: sck10 | last post by:
Hello, I am using the following in ASP.NET 2.0 VB using ADODB (which is working). I would like to convert this to csharp using ADO.NET. To build the connection, I am trying to convert: ...
5
by: RC- | last post by:
Hi everyone, I have been searching and searching for an answer to this question using Google and what not; I have not been able to find a "clear cut" answer. OK, now the question: I have a...
2
by: kendrick82 | last post by:
Hi, I am developing a web application and facing a difficulty in inserting the new data in a Datatable into the MS Access databse. The below method is able to execute without any error message but...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.