I will be back with my Active Directory tool ...
I am trying to list groups in the member attribute of the user. The following is the function I am using:
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 correct group for users belonging to only one group. As soon as there is more than one group, it returns System.Object [].
How do I do to see all groups?
source share