是用Winform做的一个Tool左边的Treeview是从AD上面拉下来的OU&Group.我把OU或者Group拉到右边时是一个Gridview显示该OU或者Group的信息都没问题.但是ParentGroups在Group有嵌套关系时居然获取的是Group自身的ID.ParentGroups这一栏应该是没有ParentGroup时为空,只显示SubGroup的ParentGroups.有多个ParentGroup时用";"隔开.
下面是相关代码,请问懂AD的大侠们该如何修改呢?
- C# code
private DataTable ADGroup;private void InitialADGroupTableSchema() { ADGroup = new DataTable("ADGSSGroup"); ADGroup.Columns.Add("ID", typeof(string)); ADGroup.Columns.Add("Name", typeof(string)); ADGroup.Columns.Add("Description", typeof(string)); ADGroup.Columns.Add("GroupType", typeof(int)); ADGroup.Columns.Add("Owner", typeof(string)); ADGroup.Columns.Add("Coowner", typeof(string)); ADGroup.Columns.Add("CreatedDate", typeof(DateTime)); ADGroup.Columns.Add("ChangedDate", typeof(DateTime)); ADGroup.Columns.Add("IsGSSGroup", typeof(bool)); ADGroup.Columns.Add("AutoAcceptOUChange", typeof(bool)); ADGroup.Columns.Add("CompanyCode", typeof(string)); ADGroup.Columns.Add("AutoExpiredDate", typeof(DateTime)); ADGroup.Columns.Add("ParentGroups", typeof(string)); }private DataRow GetDataRowFromADEntry(DirectoryEntry ADEntry) { string objName = ADEntry.Name.Remove(0, 3); // DataRow dr = ADGroup.NewRow(); dr["ID"] = objName; dr["name"] = objName; dr["Description"] = Equals(ADEntry.Properties["Description"].Value, null) ? string.Empty : ADEntry.Properties["Description"].Value.ToString(); dr["Owner"] = string.Empty; dr["Coowner"] = string.Empty; dr["IsGSSGroup"] = true; dr["AutoAcceptOUChange"] = true; dr["GroupType"] = int.Parse(ADEntry.Properties["groupType"].Value.ToString()); dr["CreatedDate"] = DateTime.Parse(ADEntry.Properties["whenCreated"].Value.ToString()); dr["ChangedDate"] = DateTime.Parse(ADEntry.Properties["whenChanged"].Value.ToString()); dr["CompanyCode"] = GetCompanyCode(ADEntry.Path); dr["AutoExpiredDate"] = DateTime.Now.AddMonths(12); DirectoryEntry deParent = ADEntry.Parent; dr["ParentGroups"] = deParent.SchemaClassName.ToLower().Equals("group") ? deParent.Name.Remove(0, 3) : string.Empty; return dr; }private void FetchNestedGroups(string groupDN) { using (var groupDE = new DirectoryEntry(ADServer + RootPath, UserName, Password, AuthenticationTypes.Secure)) { var ds = new DirectorySearcher { SearchRoot = groupDE, Filter = String.Format("(&(memberOf={0})(objectCategory=group)(objectClass=group))", groupDN), ReferralChasing = ReferralChasingOption.All }; ds.PropertiesToLoad.Add("GRPOBJID"); ds.PropertiesToLoad.Add("name"); ds.PropertiesToLoad.Add("description"); ds.PropertiesToLoad.Add("groupType"); ds.PropertiesToLoad.Add("whenCreated"); ds.PropertiesToLoad.Add("whenChanged"); ds.PropertiesToLoad.Add("distinguishedName"); foreach (SearchResult sr in ds.FindAll()) { string newID = sr.Properties["name"][0].ToString(); if (sr.Properties["GRPOBJID"].Count > 0 && !string.IsNullOrEmpty(sr.Properties["GRPOBJID"][0].ToString())) { newID = sr.Properties["GRPOBJID"][0].ToString(); } string groupName = groupDN.Split(new[] {','})[0].Remove(0, 3); DataRow dr = ADGroup.NewRow(); dr["ID"] = newID; dr["name"] = sr.Properties["name"][0]; dr["Description"] = (sr.Properties["GRPOBJID"].Count > 0) && !Equals(sr.Properties["description"][0], null) ? sr.Properties["description"][0] : string.Empty; dr["Owner"] = string.Empty; dr["Coowner"] = string.Empty; dr["IsGSSGroup"] = true; dr["AutoAcceptOUChange"] = true; dr["GroupType"] = int.Parse(sr.Properties["groupType"][0].ToString()); dr["CreatedDate"] = DateTime.Parse(sr.Properties["whenCreated"][0].ToString()); dr["ChangedDate"] = DateTime.Parse(sr.Properties["whenChanged"][0].ToString()); dr["CompanyCode"] = GetCompanyCode(sr.GetDirectoryEntry().Path); dr["AutoExpiredDate"] = DateTime.Now.AddMonths(12); dr["ParentGroups"] = newID; TryAppendRow(ADGroup, dr); FetchNestedGroups(sr.Properties["distinguishedName"][0].ToString()); } } }private void dgvADGroup_DragDrop(object sender, DragEventArgs e) { // Ctrl Pressed if ((e.KeyState & 8) != 8) { ADGroup.Rows.Clear(); } if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { var ouNode = (TreeNode) e.Data.GetData("System.Windows.Forms.TreeNode"); if (!Equals(null, ouNode.Tag)) { var adEntry = (DirectoryEntry) ouNode.Tag; if (string.Equals(adEntry.SchemaClassName.ToLower(), "group")) { DataRow drNew = GetDataRowFromADEntry(adEntry); if (TryAppendRow(ADGroup, drNew)) { FetchNestedGroups(adEntry.Properties["distinguishedName"].ToString()); } else { MessageBox.Show("Group already exists in the grid view."); } } foreach (DirectoryEntry subEntry in adEntry.Children) { if (string.Equals(subEntry.SchemaClassName.ToLower(), "group")) { DataRow dr = GetDataRowFromADEntry(subEntry); TryAppendRow(ADGroup, dr); FetchNestedGroups(subEntry.Properties["distinguishedName"][0].ToString()); } } } } ADGroup.AcceptChanges(); dgvADGroup.DataSource = ADGroup; }