活动目录 - 奇怪的行为

时间:2013-10-01 14:47:57

标签: c# active-directory principalsearcher

我正在尝试获取信息(组成员)。

我每次都收到消息“无法检索有关域的信息(1355)”

为了获得这些团体,它仅仅尝试了2次。第一次不起作用,但第二次给我带来了团体。但是为了获得一个团体的成员,我没有工作。

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "192.168.178.105:3268", "DC=ibcdev,DC=local", ContextOptions.Negotiate, "Administrator", "123");
        // define a "query-by-example" principal - here, we search for a GroupPrincipal 
        GroupPrincipal qbeGroup = new GroupPrincipal(ctx);

        // create your principal searcher passing in the QBE principal    
        PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

        // find all matches
        try
        {
            var re2s = srch.FindAll().ToList();
        }
        catch (Exception)
        {
        }
        var res = srch.FindAll();
        foreach (Principal found in res)
        {
            Console.WriteLine(found.SamAccountName);
            var group = GroupPrincipal.FindByIdentity(ctx, found.Name);
            foreach (var user in group.Members)
            {
                Console.WriteLine(user.SamAccountName);
            }
        }

有人知道我做错了吗?

此致

1 个答案:

答案 0 :(得分:0)

这是我用于在域中查找群组成员的内容:

public List<String> GetIDs(string domainName, string groupName)
{
    using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName))
    using(GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
        return (from x in grp.GetMembers(true).AsParallel() select x.SamAccountName).ToList();
}