C# 枚举DirectoryEntry组时发生操作错误

C# 枚举DirectoryEntry组时发生操作错误,c#,asp.net,active-directory,C#,Asp.net,Active Directory,我整个上午都在阅读前一篇文章,试图解决这个问题,但它们似乎都不起作用 我一直在为我们的课程管理员校正AD的用户管理界面,其想法是只显示他们所需的内容,而解决方案在开发服务器上运行良好,我在prod上得到了上述错误 我已经尝试了所有我能找到的东西,比如HostingEnvironment.Impersonate,将服务帐户提升为域管理员,但没有任何效果 public static List<GroupPrincipal> GetGroups(string client) {

我整个上午都在阅读前一篇文章,试图解决这个问题,但它们似乎都不起作用

我一直在为我们的课程管理员校正AD的用户管理界面,其想法是只显示他们所需的内容,而解决方案在开发服务器上运行良好,我在prod上得到了上述错误

我已经尝试了所有我能找到的东西,比如HostingEnvironment.Impersonate,将服务帐户提升为域管理员,但没有任何效果

public static List<GroupPrincipal> GetGroups(string client)
{
        List<GroupPrincipal> List = new List<GroupPrincipal>();

        DirectoryEntry ou = null;
        GroupPrincipal group = null;
        PrincipalContext context = null;

        if (domain.Path.ToLower().Contains(DevDN.ToLower()))
        {
            context = new PrincipalContext(ContextType.Domain,
                DevDom,
                DevDN,
                DevService,
                DevServicePass);
        }
        else
        {
            context = new PrincipalContext(
                ContextType.Domain,
                LiveDom,
                LiveDN,
                LiveService,
                LiveServicePass);
        }

        DirectorySearcher searcher = new DirectorySearcher(domain, "(&(ou=" + client + ")(objectClass=organizationalUnit))");
        try
        {
            ou = new DirectoryEntry(searcher.FindOne().Path);
        }
        catch (System.Exception ex)
        {
            Log.WriteError("SUGM.ADLink.GetGroups", "Unable to locate client: " + ex.Message);
            List = null;
            return List;
        }
        try
        {
            foreach (DirectoryEntry groups in ou.Children)
            {
                if (groups.SchemaClassName == "group")
                {
                    string name = groups.Name.Replace("CN=", "");
                    group = GroupPrincipal.FindByIdentity(context, name);
                    List.Add(group);
                }
            }
        }
        catch (System.Exception ex)
        {
            Log.WriteError("SUGM.ADLink.GetGroups", "Unable to add groups to list: " + ex.Message);
            List = null;
            return List;
        }

        return List;
    }
公共静态列表GetGroups(字符串客户端)
{
列表=新列表();
DirectoryEntry ou=null;
GroupPrincipal-group=null;
PrincipalContext上下文=null;
if(domain.Path.ToLower().Contains(DevDN.ToLower()))
{
上下文=新的PrincipalContext(ContextType.Domain,
德夫多姆,
德夫登,
DevService,
DevServicePass);
}
其他的
{
上下文=新的PrincipalContext(
ContextType.Domain,
生活世界,
LiveDN,
现场服务,
现场服务通行证);
}
DirectorySearcher search=新的DirectorySearcher(域“(&(ou=“+client+”)(objectClass=organizationalUnit))”;
尝试
{
ou=新目录条目(searcher.FindOne().Path);
}
catch(System.Exception-ex)
{
Log.WriteError(“SUGM.ADLink.GetGroups”,“找不到客户端:”+ex.Message);
列表=空;
退货清单;
}
尝试
{
foreach(ou.Children中的DirectoryEntry组)
{
if(groups.SchemaClassName==“group”)
{
字符串名称=groups.name.Replace(“CN=”,“”);
group=GroupPrincipal.FindByIdentity(上下文、名称);
列表。添加(组);
}
}
}
catch(System.Exception-ex)
{
Log.WriteError(“SUGM.ADLink.GetGroups”,“无法将组添加到列表:”+ex.Message);
列表=空;
退货清单;
}
退货清单;
}
在调试过程中,我进行了检查,并传递了所有正确的值,但它总是在foreach块上失败

谁能指出我做错了什么


干杯

您应该避免将
System.DirectoryServices
System.DirectoryServices.AccountManagement
名称空间混合使用-这不是一个很好的策略

你也可以在S.DS.AM(.NET 3.5)中做任何你想做的事情!而且也容易得多

您可以使用
PrincipalSearcher
和“示例查询”主体进行搜索:

// create your domain context and specify the initial container to work from
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=YourStartingPoint,DC=YourCompany,DC=com");

// 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
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果您还没有-请阅读MSDN文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement
中的新功能。或者查看名称空间。

为什么要将
System.DirectoryServices.AccountManagement
(.NET 3.5)名称空间中的
PrincipalContext
GroupPrincipal
System.DirectoryServices
名称空间中的
DirectorySearcher
(.NET 2.0)混合使用??没有意义,也不会很好地工作。。。请解释一下你一开始想做什么,Woops甚至没有注意到,我真淘气。你的解决方案很有效,非常感谢