public static List <DomainInformation> EnumerateUsersFromDomain(string name, UserProfile options, bool ignoreStructure = false) { var dc = EnumerateDomains(name, options); List <DomainInformation> Domains = new List <DomainInformation>(); foreach (var domain in dc) { DomainInformation newDomain = new DomainInformation() { Type = DomainInformationTypeEnum.Root, Name = (domain as Domain).Name }; var gc = DomainDiscovery.EnumerateGlobalCatalogs((domain as Domain).Name, options); foreach (var globalCatalog in gc) { if ((globalCatalog as GlobalCatalog).SiteName != string.Empty) { DirectoryEntry directoryRoot = new DirectoryEntry("LDAP://" + (globalCatalog as GlobalCatalog).Name, options.UserName, UserProfile.ToInsecureString(options.UserPassword)); if (ignoreStructure) { EnumerateOnlyUsers(newDomain, directoryRoot); } else { EnumerateUsers(newDomain, directoryRoot); } } } Domains.Add(newDomain); } return(Domains); }
private static void EnumerateOnlyGroups(DomainInformation root, DirectoryEntry directory) { using (DirectorySearcher mySearcher = new DirectorySearcher(directory) { Filter = "(objectClass=group)" }) { foreach (SearchResult node in mySearcher.FindAll()) { var item = new DomainInformation() { Type = DomainInformationTypeEnum.Group, Name = node.GetDirectoryEntry().Name.Replace("CN=", ""), Description = node.GetDirectoryEntry().Properties["description"].Value != null?node.GetDirectoryEntry().Properties["description"].Value.ToString() : string.Empty, Info = ParseGroupInfo(node) }; if (node.Properties["member"] != null) { foreach (var member in node.Properties["member"]) { (item.Info as ActiveDirectoryGroupInfo).Members.Add(member.ToString().Split(',')[0].Replace("CN=", "")); } } (item.Info as ActiveDirectoryGroupInfo).SourceName = root.Name; root.Childrens.Add(item); } } }
public static DomainInformation EnumerateGroupsFromDomainController(string name, UserProfile options, bool ignoreStructure = false) { DomainInformation domainController = new DomainInformation() { Type = DomainInformationTypeEnum.Root, Name = name }; DirectoryEntry directoryRoot = new DirectoryEntry("LDAP://" + name, options.UserName, UserProfile.ToInsecureString(options.UserPassword)); if (ignoreStructure) { EnumerateOnlyGroups(domainController, directoryRoot); } else { EnumerateGroups(domainController, directoryRoot); } return(domainController); }
private static void EnumerateOnlyUsers(DomainInformation root, DirectoryEntry directory) { using (DirectorySearcher mySearcher = new DirectorySearcher(directory) { Filter = "((&(objectCategory=Person)(objectClass=User)))" }) { foreach (SearchResult node in mySearcher.FindAll()) { var item = new DomainInformation() { Type = DomainInformationTypeEnum.User, Name = node.GetDirectoryEntry().Name.Replace("CN=", ""), Description = node.GetDirectoryEntry().Properties["description"].Value != null?node.GetDirectoryEntry().Properties["description"].Value.ToString() : string.Empty, Info = ParseUserInfo(node) }; (item.Info as ActiveDirectoryUserInfo).SourceName = root.Name; root.Childrens.Add(item); } } }
private static void EnumerateGroups(DomainInformation root, DirectoryEntry directory) { foreach (DirectoryEntry child in directory.Children) { if (child.SchemaClassName == "organizationalUnit" || child.SchemaClassName == "container" || child.SchemaClassName == "group") { DirectorySearcher mySearcher = new DirectorySearcher(child) { Filter = "(objectClass=group)" }; if (mySearcher.FindAll().Count != 0 || child.SchemaClassName == "group") { switch (child.SchemaClassName) { case "organizationalUnit": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.OrganizationUnit, Name = child.Name.Replace("OU=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { root.Childrens.Add(item); EnumerateGroups(item, child); } break; } case "container": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.OrganizationUnit, Name = child.Name.Replace("CN=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { root.Childrens.Add(item); EnumerateGroups(item, child); } break; } case "group": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.Group, Name = child.Name.Replace("CN=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty, Info = ParseGroupInfo(child) }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { if (child.Properties["member"] != null) { foreach (var member in child.Properties["member"]) { (item.Info as ActiveDirectoryGroupInfo).Members.Add(member.ToString().Split(',')[0].Replace("CN=", "")); } } root.Childrens.Add(item); } break; } } } else { continue; } } else { continue; } } }
private static void EnumerateUsers(DomainInformation root, DirectoryEntry directory) { foreach (DirectoryEntry child in directory.Children) { if (child.SchemaClassName == "organizationalUnit" || child.SchemaClassName == "container" || child.SchemaClassName == "user") { DirectorySearcher mySearcher = new DirectorySearcher(child) { Filter = "((&(objectCategory=Person)(objectClass=User)))" }; if (mySearcher.FindAll().Count != 0 || child.SchemaClassName == "user") { switch (child.SchemaClassName) { case "organizationalUnit": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.OrganizationUnit, Name = child.Name.Replace("OU=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { root.Childrens.Add(item); EnumerateUsers(item, child); } break; } case "container": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.OrganizationUnit, Name = child.Name.Replace("CN=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { root.Childrens.Add(item); EnumerateUsers(item, child); } break; } case "user": { var item = new DomainInformation() { Type = DomainInformationTypeEnum.User, Name = child.Name.Replace("CN=", ""), Description = child.Properties["description"].Value != null ? child.Properties["description"].Value.ToString() : string.Empty, Info = ParseUserInfo(child) }; if (root.Childrens.Where(x => x.Name == item.Name && x.Description == item.Description).Count() == 0) { root.Childrens.Add(item); } break; } } } else { continue; } } else { continue; } } }