我有下面的代码从 Active Directory 中检索组。当我只为MY_GROUP_NAME(在下面注释掉)运行代码时,输出符合预期。当我从 AD 运行完整的一组组时,最终的数据集是不正确的。一个具体的例子是,我最终在列表中得到了多个具有相同组名但 ParentGroupGuid 不同的 adGroup。这是一个无效的场景。这个问题似乎与Parallel.ForEach()调用下面的递归方法有关。知道问题可能是什么以及如何解决吗?private ConcurrentBag<Core.Models.ADGroup> adGroups; public async Task<List<Core.Models.ADGroup>> GetADGroupsFromADAsync(string domainName){ return await Task.Run(async() => { var domainId = await new DomainRepository().GetDomainId(domainName); using (var context = new PrincipalContext(ContextType.Domain, domainName)) { var ps = new PrincipalSearcher(new GroupPrincipal(context)); Parallel.ForEach( ps.FindAll().ToList(), //ps.FindAll().Where(x => x.Name == "MY_GROUP_NAME").ToList(), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, async (group, loopState) => { await GetGroupsRecursive((Guid)domainId, null, (GroupPrincipal)group); }); } //return group list return adGroups.ToList(); });}private async Task GetGroupsRecursive(Guid domainId, Guid? parentGroupGuid, GroupPrincipal group){ //cast result to adgroup var adGroup = Mapper.Map<Core.Models.ADGroup>(group); //set domainid adGroup.DomainId = domainId; //set parent group id adGroup.ParentGroupGuid = parentGroupGuid; //process child groups foreach (var member in group.Members) if (member is GroupPrincipal) await GetGroupsRecursive(domainId, adGroup.Guid, (GroupPrincipal)member); //add to the list adGroups.Add(adGroup);}
1 回答
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报
0/150
提交
取消