大家好,我在从递归函数调用中获取正确结果时遇到麻烦
所以这就是我所拥有的
public IEnumerable<AdProfileDto> GetAdProfilesByGroup(string groupDisplayName)
{
if (!string.IsNullOrEmpty(groupDisplayName))
{
IList<AdProfileDto> adProfiles = null;
/// start AD query
using (DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry(AdHelper.path)))
{
/// configure searcher for group
/// https://msdn.microsoft.com/en-us/library/aa746475(v=vs.85).aspx
searcher.Filter = string.Format("(&((|(&(objectclass=user)(objectcategory=person))(objectcategory=group)))(cn={0}))", AdHelper.ReplaceSpecialChars(groupDisplayName)); //--> exact search
searcher.PropertiesToLoad.AddRange(new[] { AdHelper.member, AdHelper.category, AdHelper.displayName, AdHelper.accountName });
searcher.SearchScope = SearchScope.Subtree;
SearchResult result = searcher.FindOne();
if (result != null) //--> check if there are any results
{
if (AdHelper.IsGroup((string)result.Properties[AdHelper.category][0])) //--> check if result is a group
{
ResultPropertyValueCollection propertyValues = result.Properties[AdHelper.member]; //--> get all members for this group : users / groups / both
//for (int index = 0; index < propertyValues.Count; ++index)
//{
return GetAdProfilesByGroup(AdHelper.GetPropertyCN((string)propertyValues[0])); //--> recursive check each member: user | group, extract displayName first
//}
}
else
{
//return GetAdProfilesByCN((string)result.Properties[AdHelper.displayName][0], 1);
adProfiles = new List<AdProfileDto>(); //--> initialize list
adProfiles.Add(GetAdProfilesByCN((string)result.Properties[AdHelper.displayName][0], 1).FirstOrDefault());
}
}
return adProfiles;
}
}
return null;
}
我的想法是,我从活动目录中获取一些数据,我有2套我感兴趣的:群组和用户。一个组可以有其他组和/或用户。最终结果应该是特定组的用户列表,如果有的话,还应该是其子组的用户列表 例如:
GroupeA:user1,user2 =&gt;
返回GroupeB:groupZ,user1 =&gt;结果将是user1,user3,user4,user5
|
-> user 3, user 4, groupT
|
-> user 5
任何帮助? 谢谢。
P.S。目前我只得到一个结果:在检查了同时拥有用户和组(其他用户和子组)的组后,迭代看起来像:
Groupe1:GroupeA,GroupeB,GroupeC,User1,User2 ... UserN
GroupeA:UserA1,UserA2 .. UserAN(仅限)
返回UserA1并且函数结束它不会迭代其余的GroupA用户,也不会在GroupeB级别的呼叫结束后恢复该功能
答案 0 :(得分:0)
考虑以下问题的精简版本:
public interface IMember
{
bool IsGroup { get; }
}
public interface IUser : IMember { }
public interface IGroup: IMember
{
IEnumerable<IMember> Members { get; }
}
枚举任何给定组的所有用户(包括属于可能的嵌套组的用户)的方法如下:
public static IEnumerable<IMember> GetAllMembers(this IGroup group)
{
foreach (var member in group.Members)
{
if (member.IsGroup)
{
var nestedGroup = member as IGroup;
foreach (var nestedMember in nestedGroup.GetAllMembers())
{
yield return nestedMember;
}
}
else
{
yield return member;
}
}
}