Active Directory检索组中的组成员似乎陷入了无限循环

时间:2019-05-21 17:28:20

标签: c# while-loop active-directory infinite-loop

我在下面有以下代码。每次调用directoryEntryGroupMembers.Properties["member"];时,它都会返回相同的列表。结果,while循环陷入了无限循环。我怀疑如何在AD本身而不是代码中设置组。有人知道如何解决吗?

注意:我知道我们广告中的圆形嵌套组,但是到现在为止还没有引起问题。因此,我不知道圆形组嵌套是否是问题所在。

string strDirectoryEntryPath = null;
const int intIncrement = 1500; // https://msdn.microsoft.com/en-us/library/windows/desktop/ms676302(v=vs.85).aspx
string strDsFilter = null;
string[] strDsProperties = null;
List<Guid> lstGroupMemberGuid = null;
DataTypeConverter objConverter = null;
DirectoryEntry directoryEntryGroupMembers = null;
SearchResultCollection searchResultCollection = null;
DirectorySearcher directorySearcher = null; 

objConverter = new DataTypeConverter();

lstGroupMemberGuid = new List<Guid>();

strDirectoryEntryPath = strActiveDirectoryHost + "/<GUID=" + strPropertyValue + ">";

directoryEntryGroupMembers = new DirectoryEntry(strDirectoryEntryPath, null, null, AuthenticationTypes.Secure);

while (true)
{
    intCountIteration = intCountIteration + 1;

    var varGroupMemberDns = directoryEntryGroupMembers.Properties["member"];

    foreach(string strMemberDn in varGroupMemberDns)
    {
        if (dctObjectGuidDn.TryGetValue(strMemberDn, out Guid gidMemberGuid))
        {
            lstGroupMemberGuid.Add(gidMemberGuid);
        }
    }

    if (varGroupMemberDns.Count < intIncrement)
    {
        break;
    }

    directoryEntryGroupMembers.RefreshCache(new[] { string.Format("member;range={0}-*", lstGroupMemberGuid.Count) });
}

1 个答案:

答案 0 :(得分:1)

问题在于varGroupMemberDns.Count不会减少,也不会减少。我相信您可能想要在循环之前捕获计数,然后在每次迭代时减少计数。

var recordCount = varGroupMemberDns.Count;
while (true)
{
recordCount--;
...
  if (recordCount < intIncrement)
  {

  }
  ...
}