PrincipalSearchResult和System.OutOfMemoryException

时间:2018-12-05 02:58:51

标签: c# active-directory out-of-memory principalcontext

我正在使用Domain PrincipalContext查找用户组。我明白了。但是,当我尝试使用组集合时,会得到System.OutOfMemoryException。所有主要对象都是一次性的。在我的代码中有using部分。我曾尝试使用Dispose()方法和GC.Collect()方法,但这无济于事。

这是代码:

using (var ctx = new PrincipalContext(ContextType.Domain, _domain, _user, _password))
{
    using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx,IdentityType.SamAccountName, sAMAccountName))
    {
        PrincipalSearchResult<Principal> userGroups = user.GetGroups();                    
        using (userGroups)
        {
            foreach (Principal p in userGroups)
            {
                using (p)
                {
                    result.Add(p.Guid == null ? Guid.Empty : (Guid)p.Guid);
                }
            }
        }
    }
}

foreach循环返回异常。甚至foreach都是空循环。

1 个答案:

答案 0 :(得分:0)

我发现System.DirectoryServices.AccountManagement名称空间(UserPrincipal等)确实浪费了很多内存。例如,每次创建UserPrincipalGroupPrincipal时,它都会向AD询问具有值的每个属性-即使您只使用了其中一个。

如果用户是许多小组的成员,则可能是原因,尽管我仍然感到惊讶。也许您的计算机没有足够的内存来加载所有这些内容。

您可以通过直接使用System.DirectoryServices名称空间(无论如何,AccountManagement名称空间在后台使用)来做相同的事情,并使用更少的内存(可能花费更少的时间)。

这里是一个示例,它将查看用户的memberOf属性以找到组并拉出Guid。这确实有一些限制,我将描述我写的一篇文章:Finding all of a user’s groups(此示例是该文章中的一个示例)。但是,在大多数情况下(特别是如果您的环境中只有一个域而没有受信任的域),就可以了。

public static IEnumerable<Guid> GetUserMemberOf(DirectoryEntry de) {
    var groups = new List<Guid>();

    //retrieve only the memberOf attribute from the user
    de.RefreshCache(new[] {"memberOf"});

    while (true) {
        var memberOf = de.Properties["memberOf"];
        foreach (string group in memberOf) {
            using (var groupDe = new DirectoryEntry($"LDAP://{group.Replace("/", "\\/")}") {
                groupDe.RefreshCache(new[] {"objectGUID"});
                groups.Add(new Guid((byte[]) groupDe.Properties["objectGUID"].Value));
            }
        }

        //AD only gives us 1000 or 1500 at a time (depending on the server version)
        //so if we've hit that, go see if there are more
        if (memberOf.Count != 1500 && memberOf.Count != 1000) break;

        try {
            de.RefreshCache(new[] {$"memberOf;range={groups.Count}-*"});
        } catch (COMException e) {
            if (e.ErrorCode == unchecked((int) 0x80072020)) break; //no more results

            throw;
        }
    }
    return groups;
}

您需要向它提供用户的DirectoryEntry对象。如果您事先知道distinguishedName,则可以使用它(例如new DirectoryEntry($"LDAP://{distinguishedName}"))。但是,如果没有,您可以搜索它:

var ds = new DirectorySearcher(
                new DirectoryEntry($"LDAP://{_domain}"),
                $"(&(objectClass=user)(sAMAccountName={sAMAccountName}))");
ds.PropertiesToLoad.Add("distinguishedName"); //add at least one attribute so it doesn't return everything

var result = ds.FindOne();
var userDe = result.GetDirectoryEntry();

我注意到您还将用户名和密码传递给PrincipalContext。如果需要,constructor for DirectoryEntry确实接受用户名和密码,因此您可以在每次创建新的DirectoryEntry时更新此代码,以包括该代码。