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

时间:2016-03-10 10:36:11

标签: c# active-directory ldap ldap-query active-directory-group

从组中获取用户时,将异常消息作为"在尝试解析跨存储引用时,无法解析目标主体的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)

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

3 个答案:

答案 0 :(得分: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 :(得分: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);
    }
}

答案 2 :(得分:0)

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

因此,如果UserPrincipalGroupPrincipalforeach(var principal in groupMembers) ,您可能需要检查。

var scaling = [10, 10, 10, 1, 1, 1, 1];
var data = {
    labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: [5, 5, 9, 81, 56, 55, 40].map(function (e, i) {
                return e * scaling[i];
            })
        }
    ]
};

var ctx = document.getElementById("myChart").getContext("2d");
var myRadarChart = new Chart(ctx).Radar(data, {
    tooltipTemplate: function (valueObject) {
        return valueObject.value / scaling[data.labels.indexOf(valueObject.label)];
    }
});
在你的情况下,

会是更好的选择。