DataTable dtUser = GetEmptyDT(); Dictionary<DirectoryEntry, string> test1 = GetUserAndGroup(clsConst.enumADName.AD_1); foreach (DirectoryEntry deUser in test1.Keys) { string groupname = test1[deUser]; string sAMAccountName = GetProperty(deUser, "sAMAccountName"); string mail = GetProperty(deUser, "mail"); string department = GetProperty(deUser, "Department"); string displayName = GetProperty(deUser, "displayName"); string givenName = GetProperty(deUser, "givenName"); string sn = GetProperty(deUser, "sn"); DataRow dr = dtUser.NewRow(); dr["ADName"] = clsConst.enumADName.AD_1; dr["GroupName"] = groupname; dr["LoginName"] = sAMAccountName; dr["Department"] = department; dr["MailAddress"] = mail; dr["DisplayName"] = displayName; dr["FirstName"] = givenName; dr["LastName"] = sn; dtUser.Rows.Add(dr); } ExportLog(dtUser, "UserInformation_log", ConfigurationManager.AppSettings["UserInformationPath"]); public static Dictionary<DirectoryEntry, string> GetUserAndGroup(string ADName) { string[] GroupName = { string.Empty }; string path = string.Empty; string username = string.Empty; string password = string.Empty; if (ADName.Equals(clsConst.enumADName.AD_1)) { path = ConfigurationManager.AppSettings["1Path"]; username = ConfigurationManager.AppSettings["1UserName"]; password = ConfigurationManager.AppSettings["1Password"]; GroupName = ConfigurationManager.AppSettings["1Groups"].Split(','); } if (ADName.Equals(clsConst.enumADName.AD_2)) { path = ConfigurationManager.AppSettings["2Path"]; username = ConfigurationManager.AppSettings["2UserName"]; password = ConfigurationManager.AppSettings["2Password"]; GroupName = ConfigurationManager.AppSettings["2Groups"].Split(','); } if (ADName.Equals(clsConst.enumADName.AD_3)) { path = ConfigurationManager.AppSettings["3Path"]; username = ConfigurationManager.AppSettings["3UserName"]; password = ConfigurationManager.AppSettings["3Password"]; GroupName = ConfigurationManager.AppSettings["3Groups"].Split(','); } List<SearchResult> results = new List<SearchResult>(); DirectoryEntry de = GetDirectoryObject(ADName); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; string strFilter = string.Empty; if (GroupName.Length <= 0) { return null; } strFilter = "(&(objectClass=group){0}(cn=" + GroupName[0] + "){1})"; string s1 = string.Empty; string s2 = string.Empty; if (GroupName.Length > 1) { s1 = "(|"; for (int i = 1; i < GroupName.Length; i++) { s2 += "(cn=" + GroupName[i] + ")"; } s2 += ")"; } strFilter = string.Format(strFilter, s1, s2); deSearch.Filter = strFilter; deSearch.SearchScope = SearchScope.Subtree; SearchResultCollection searchResults = deSearch.FindAll(); Dictionary<DirectoryEntry, string> groupAndUser = new Dictionary<DirectoryEntry, string>(); List<string> dnstr = new List<string>(); foreach (SearchResult searchResult in searchResults) { DirectoryEntry deGroup = new DirectoryEntry(searchResult.Path, username, password, AuthenticationTypes.Secure); System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties; int n = pcoll["member"].Count; for (int l = 0; l < n; l++) { DirectoryEntry deUser = new DirectoryEntry(path + "/" + pcoll["member"][l].ToString(), username, password, AuthenticationTypes.Secure); string dn = deUser.Properties["distinguishedName"][0].ToString(); if (!dnstr.Contains(dn)) { dnstr.Add(dn); groupAndUser.Add(deUser,deGroup.Name.Remove(0,3)); } } } return groupAndUser; }