public static Principal FromDirectoryEntry(DirectoryEntry directoryEntry, ICollection<string> additionalPropertyNames = null) { if (directoryEntry == null) { throw new ArgumentNullException("directoryEntry"); } if (directoryEntry.Properties[ActiveDirectoryProperties.AccountName].Value == null) { throw new ActiveDirectoryException("Account name property not found in active directory entry."); } bool isGroup = directoryEntry.SchemaClassName == ActiveDirectoryProperties.AccountGroup; var principal = new Principal { AccountName = directoryEntry.Properties[ActiveDirectoryProperties.AccountName].Value.ToString(), NativeGuid = new Guid(directoryEntry.NativeGuid), DomainPath = directoryEntry.Path, IsGroup = isGroup, IsActive = isGroup || IsActiveUser(directoryEntry) }; principal.DisplayName = GetDisplayName(directoryEntry) ?? principal.AccountName; principal.AdditionalProperties = GetAdditionalProperties(directoryEntry, additionalPropertyNames); principal.ChildDomainPaths = GetChildDomainPaths(directoryEntry); return principal; }
protected virtual IReadOnlyCollection<IPrincipal> ResolveMembers(Principal parent, bool isRecursive, ICollection<string> additionalPropertyNames) { if (!parent.IsGroup) { return new IPrincipal[0]; } var results = new List<IPrincipal>(); foreach (string childDomainPath in parent.ChildDomainPaths) { string fullChildDomainPath = domainPath.GetPathWithProtocol() + "/" + childDomainPath; if (!results.Any(user => user.DomainPath.Equals(fullChildDomainPath))) { using (var childDirectoryEntry = new DirectoryEntry(fullChildDomainPath)) { Principal childPrincipal = Principal.FromDirectoryEntry(childDirectoryEntry, additionalPropertyNames); results.Add(childPrincipal); if (isRecursive) { IEnumerable<IPrincipal> childPrincipalMembers = ResolveMembers(childPrincipal, true, additionalPropertyNames); results.AddRange(childPrincipalMembers.Where(p => results.All(r => r.NativeGuid != p.NativeGuid))); } } } } return results; }