I'am back with my Active Directory tool...
I'am trying to list the groups in the "member of" attribute of a user. Below is the function I use:
public static DataTable ListGroupsByUser(string selectedOu)
{
DataTable groupListByUser = new DataTable();
String dom = "OU=" + selectedOu + ",OU=XXX,DC=XXX,DCXXX,DC=XXX,DC=XXX";
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + dom);
DataColumn column;
DataRow row;
column = new DataColumn();
column.ColumnName = "ID";
groupListByUser.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "User";
groupListByUser.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Groups";
groupListByUser.Columns.Add(column);
int i = 1;
foreach (DirectoryEntry child in directoryObject.Children)
{
row = groupListByUser.NewRow();
groupListByUser.Rows.Add(row);
row["ID"] = i++;
if (child.Properties["memberOf"].Value != null)
{
row["User"] = child.Properties["sAMAccountName"].Value.ToString();
row["Groups"] = child.Properties["memberOf"].Value.ToString();
}
else
{
row["Groups"] = "blabla";
}
}
return groupListByUser;
}
It returns the right group for users belonging to only one group. As soon as There's more than one group, it returns System.Object[].
How can I do to see all groups ?