1 /// <summary>
2 /// Get all members belonging to Domain Group.
3 ///
4 /// Group member searches require credentials to work.
5 /// </summary>
6 /// <param name="DomainName">string. The group's domain name. Example "AMERICAS"</param>
7 /// <param name="GroupName">string. Active Directory groupe. Do not include the domain. Example: "MfgBuildDev"</param>
8 /// <param name="GroupOwner">string. Owner of group.</param>
9 /// <returns></returns>
10 public static string[] GetGroupMembers(
11 string DomainName,
12 string GroupName,
13 out string GroupOwner)
14 {
15 try
16 {
17 // get the user name and password to use with directory integration
18 ConfigSettings Cs = new ConfigSettings();
19 ConfigSettings.ResourceConfig Cfg = Cs.GetResourceInformation(ConfigSettings.ResourceAlias.DIRECTORY_INTEGRATION);
20
21 GroupOwner = string.Empty;
22 DirectoryEntry Entry = new DirectoryEntry("LDAP://" + DomainName);
23 Entry.Username = Cfg.UserName;
24 Entry.Password = Cfg.Password;
25 DirectorySearcher Search = new DirectorySearcher(Entry);
26 Search.Filter = "(&(objectCategory=group)(cn=" + GroupName + "))";
27 Search.PropertiesToLoad.Add("description");
28 Search.PropertiesToLoad.Add("distinguishedname");
29 SearchResult Result = Search.FindOne();
30 ArrayList Members = new ArrayList(100);
31 if (Result != null)
32 {
33 GroupOwner = Result.Properties["description"][0].ToString();
34 DirectoryEntry Group =
35 new DirectoryEntry("LDAP://" + Result.Properties["distinguishedname"][0].ToString());
36 Group.Username = Cfg.UserName;
37 Group.Password = Cfg.Password;
38 foreach (object Dn in Group.Properties["member"])
39 {
40 // examples of LDAP strings returned for members:
41 string Member = Dn.ToString().Replace("CN=", string.Empty).Replace("\\, ", "|");
42 Member = Member.Substring(0, Member.IndexOf(",OU="));
43 // if the name is backwards, flip it around
44 if (Member.IndexOf("|") != -1)
45 {
46 string[] Parts = Member.Split('|');
47 Member = (Parts[1] + "_" + Parts[0]).Replace(" ", "_");
48 }
49 string Domain = Dn.ToString().Substring(Dn.ToString().IndexOf("DC="));
50 Domain = Domain.Replace("DC=", string.Empty).Replace(",", ".");
51 Members.Add(string.Format("{0}\\{1}", Domain, Member)); // return members with domain
52 }
53 }
54
55 return (string[])Members.ToArray(typeof(string));
56 }
57 catch (Exception ex)
58 {
59 //Error = ex;
60 Logging.LogException(MethodBase.GetCurrentMethod(), ref ex);
61 GroupOwner = string.Empty;
62 return null;
63 }
64 }