C# 为什么GroupPrincipal.FindByIdentity即使知道组存在也返回null?

C# 为什么GroupPrincipal.FindByIdentity即使知道组存在也返回null?,c#,active-directory,C#,Active Directory,为什么会 GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(), "TEST_DESTINATION_GRP"); 返回null?我知道集团组织单位下存在TEST\u DESTINATION\u GRP 我的代码: private void addUserToGroup(string

为什么会

GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(), 
                                                         "TEST_DESTINATION_GRP");
返回
null
?我知道集团组织单位下存在
TEST\u DESTINATION\u GRP

我的代码:

private void addUserToGroup(string userName, string groupName)
{
    try
    {
        UserPrincipal user = UserPrincipal.FindByIdentity(getPrincipalContext(), IdentityType.SamAccountName, "jcolon");
        GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(), "TEST_DESTINATION_GRP");
        //just to show that I can access AD            
        ArrayList x = getUserGroups(userName);

        foreach (var xy in x)
        {
            Console.WriteLine(xy);
        }//I can access AD FINE

        if (group == null)
        { Console.WriteLine("Wtf!"); }
        Console.WriteLine(user); 
        Console.WriteLine(group + "empty why!!!!");
    }
    catch (Exception e)
    { 
        //log e
    }
}

private PrincipalContext getPrincipalContext()
{
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain,sDefaultOU);          
    return oPrincipalContext;
}   
上述代码的输出为:

Domain Users GRP_ADMIN_SERVER GRP_PROG_III CTXXA-FlexUser CTXXA-Users Wtf Abanico, Elnora@MSH empty why!!!! 域用户 GRP_管理_服务器 GRP_PROG_III CTXXA FlexUser CTXXA用户 世界跆拳道联盟 阿巴尼科,Elnora@MSH 空为什么!!!!
有什么想法吗?

您的
getPrincipalContext
方法中的
sDomain
sDefaultOU
中的值是什么

我将尝试以下步骤:

  • 构建您的
    PrincipalContext
    ,不使用任何域名或OU名称-在这种情况下,将使用默认域及其顶级节点:

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    现在搜索-它能找到你要找的群吗

  • 检查以确保域和
    sDefaultOU
    值正确-如果将主要上下文连接到一个OU,则显然无法在另一个OU中搜索(除非该OU是您连接到的OU的子OU)