[英]Active Directory: How to get a list of users according to a specific Organization unit
I'm looking for some professionnals for a bit of help... I'm quite new in development, and I'm working on my first project at work. 我正在寻找一些专业人士以寻求帮助...我在开发中还很新,我正在工作中的第一个项目。 The goal is to automatically delete the non-needed accounts in the active directory (in a specific OU (org. unit) of course). 目标是自动删除活动目录(当然是在特定的OU(组织单位)中)中不需要的帐户。 For this to be done, I need to get the list of the accounts. 为此,我需要获取帐户列表。 This works fine. 这很好。 I'm working with three tiers (Data Access, Business and GUI). 我正在使用三个层次(数据访问,业务和GUI)。
I wrote a 'GetOU' function in the data access layer, wich get the OU from the Active Directory. 我在数据访问层中编写了一个“ GetOU”函数,以便从Active Directory中获取OU。 The differents OU are listed in a ComboBox. 在ComboBox中列出了不同的OU。 When the user changes the OU in the combobox, I would like to get back the value of the selection, and pass it to my Data access layer, so, in a GetMembers function, I can select the members from the specified OU and throw it back to a datagrid. 当用户在组合框中更改OU时,我想取回选择的值,并将其传递给我的数据访问层,因此,在GetMembers函数中,我可以从指定的OU中选择成员并将其抛出回到数据网格。
I don't know if this is clear enough Here's what i wrote till now: Data Access: 我不知道这是否足够清楚这是我到目前为止所写的内容:数据访问:
public static DataTable GetMembers()
{
DataTable membersDt = new DataTable();
DataSet membersDs = new DataSet();
DataColumn column;
column = new DataColumn();
column.ColumnName = "ID";
membersDt.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "First Name";
membersDt.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Last Name";
membersDt.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Logon";
membersDt.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "ManagerDN";
membersDt.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Logon Manager";
membersDt.Columns.Add(column);
membersDs.Tables.Add(membersDt);
DataRow row;
String dom = "OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=xxx,DC=xxx";
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + dom);
int i = 1;
foreach (DirectoryEntry child in directoryObject.Children)
{
row = membersDt.NewRow();
membersDt.Rows.Add(row);
row["ID"] = i++;
if (child.Properties["givenName"].Value == null)
{
row["First Name"] += "Group Logon";
}
else
{
row["First Name"] = child.Properties["givenName"].Value.ToString();
}
row["Last Name"] = child.Properties["sn"].Value.ToString();
row["Logon"] = child.Properties["sAMAccountName"].Value.ToString();
if (child.Properties["Manager"].Value == null)
{
row["ManagerDN"] += "Unknown Manager";
}
else
{
row["ManagerDN"] = child.Properties["Manager"].Value.ToString();
}
String uManager = row["ManagerDN"].ToString();
DirectoryEntry UserManager = new DirectoryEntry("LDAP://" + uManager);
if (child.Properties["Manager"].Value == null)
{
row["Logon manager"] += "Unknown Manager";
}
else
{
row["Logon Manager"] = UserManager.Properties["sAMAccountName"].Value.ToString();
}
}
return membersDt;
}
public static DataTable GetOUList()
{
//Creation du filtre de recherche
String dom = "OU=xxx,DC=xxx,DC=xxx,DC=xxx,DC=xxx";
DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + dom);
DirectorySearcher ouSearcher = new DirectorySearcher(directoryObject);
ouSearcher.Filter = "(objectClass=OrganizationalUnit)";
//Tri ascendant de la liste sortie
SortOption sortedOuList = new SortOption();
sortedOuList.PropertyName = "OU";
sortedOuList.Direction = SortDirection.Ascending;
ouSearcher.Sort = sortedOuList;
//Recherche des OU présentes dans 'string dom'
DataTable OuDt = new DataTable();
DataColumn column;
column = new DataColumn();
column.ColumnName = "OuName";
OuDt.Columns.Add(column);
DataRow row;
foreach (SearchResult result in ouSearcher.FindAll())
{
row = OuDt.NewRow();
OuDt.Rows.Add(row);
DirectoryEntry dirEntry = result.GetDirectoryEntry();
row["OuName"] = dirEntry.Properties["ou"].Value;
}
return OuDt;
}
Business: 商业:
public static DataTable GetMembers()
{
try
{
return DAL.Classes.DataProvider.GetMembers();
}
catch (Exception ex)
{
MessageBox.Show("Error getting MemberList: " + ex.Message.ToString());
throw ex;
}
}
public static DataTable GetOu()
{
try
{
return DAL.Classes.DataProvider.GetOUList();
}
catch (Exception ex)
{
MessageBox.Show("Error getting Ou: " + ex.Message.ToString());
throw ex;
}
}
GUI: 界面:
private void Form1_Load(object sender, EventArgs e)
{
//Display des OU dans le combo
OuCBox.DataSource = ToBeDeleted.BLL.Classes.MembersManager.GetOu();
OuCBox.DisplayMember = "OuName";
}
private void getListBtn_Click(object sender, EventArgs e)
{
MembersDG.DataSource = ToBeDeleted.BLL.Classes.MembersManager.GetMembers();
MembersDG.Columns["ManagerDN"].Visible = false;
MembersDG.Columns["ID"].Width = 35;
}
So I can display the memberList if the string 'dom' is fully complete. 因此,如果字符串“ dom”完整完成,我可以显示memberList。 What I wanna do is: Selection of one OU in the OuCBox, return the value to the data access layer so I can make a FindAll() on this specific OU. 我想做的是:在OuCBox中选择一个OU,将值返回到数据访问层,以便我可以在此特定OU上创建FindAll()。
Any help appreciated ! 任何帮助表示赞赏! Thanks a lot 非常感谢
Add the selected ou to the dom
variable. 将所选的ou添加到dom
变量。 Don't filter on it. 不要对此进行过滤。
OK, it's working fine now with: 好的,现在可以使用:
string selectedOu = ((System.Data.DataRowView)OuCBox.SelectedValue)).Row.ItemArray[0].ToString();
Thank you ! 谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.