在尝试解析跨存储引用时,无法解析目标主体的SID。错误代码是1332

问题描述 投票:1回答:3

从组中提取用户时,将异常消息显示为“在尝试解析跨存储引用时,无法解析目标主体的SID。错误代码为1332.”

        PrincipalContext ctx = null;
        if (!string.IsNullOrWhiteSpace(adUserName))
        {
            ctx = new PrincipalContext(ContextType.Domain, domainName, adUserName, adPassword);
        }
        else
        {
            ctx = new PrincipalContext(ContextType.Domain, domainName);
        }
        var groupNames = commaSeparatedGroupNames.Split(',');
        IEnumerable<Principal> users = null;
        foreach (var groupName in groupNames)
        {
            if (!string.IsNullOrWhiteSpace(groupName))
            {
                var userGroup = GroupPrincipal.FindByIdentity(ctx, groupName.Trim());
                if (userGroup == null)
                    throw new InvalidOperationException("Active Directory Group Not Found :: " + groupName);

                var usersInGroup = userGroup.GetMembers();

                if (users == null)
                {
                    users = usersInGroup;
                }
                else
                {
                    users = users.Union(usersInGroup);
                }
            }
        }

        return users;

做的时候

foreach (UserPrincipal user in users)

我收到了错误。我可以检查此错误的任何建议或在循环期间从列表中跳过此成员。

c# active-directory ldap ldap-query active-directory-group
3个回答
2
投票

我昨天刚遇到同样的问题,这是我在这个link找到的最佳答案:

IEnumerator<Principal> enumerator = members.GetEnumerator();
while (enumerator.MoveNext())
{
    try
    {
        Principal member = enumerator.Current;
        Console.WriteLine({0}\r\n\t{1}\r\n\t{2}",member.ToString(),member.Guid,member.DistinguishedName);
    } catch (Exception ex) {
        Console.WriteLine(ex.Message);
    }
}

这就是你如何“手动”迭代IEnumerable集合。它使您有机会尝试获取Principal并捕获异常(如果它是未定义的SID或其他问题)。


1
投票

Sandra的解决方案几乎是正确的,但MoveNext()方法抛出异常,所以如果你将try..catch块放在其中,它将无法工作。

var enumerator = members.GetEnumerator();

var moveNext = true;

while (moveNext)
{
    try
    {
        moveNext = enumerator.MoveNext())

        if (moveNext)
        {
            Principal member = enumerator.Current;

            Console.WriteLine("{0}\r\n\t{1}\r\n\t{2}", member, member.Guid, member.DistinguishedName);
        }
    } 
    catch (Exception ex) 
    {
        Console.WriteLine(ex.Message);
    }
}

0
投票

我认为你的问题与group.GetMembers()的返回类型有关,UserPrincipal不一定是Principal而是Principal

所以你可能想检查,如果UserPrincipalGroupPrincipalforeach(var principal in groupMembers)

qazxswpoi

在你的情况下,这将是一个更好的选择。

© www.soinside.com 2019 - 2024. All rights reserved.