AccountManagement 是 Principal GroupPrincipal?

AccountManagement is Principal GroupPrincipal?

如何取一个principal,看看是不是一个group?或者它有成员?

using(var ctx = new PrincipalContext(ContextType.Domain, "some.domain.com", "DC=some,DC=domain,DC=com"))
{
    var group = GroupPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, "some long distinguishedname");
    if(group != null)
    {
        var subgroups = group.GetMembers().Where(m => m.[IS A GROUP])
        foreach (var principal in group.GetMembers())
        {
            Console.WriteLine(principal.DistinguishedName);
        }
    }

}

您可以使用 as 关键字 "convert" Principal - 如果它有效,如果该对象确实是 GroupPrincipal,您将获得一个有效值, 否则 null:

var group = GroupPrincipal.FindByIdentity(ctx, 
                                          IdentityType.DistinguishedName, 
                                          "some long dn") as GroupPrincipal;
                                                          ****************** 

if (group != null)
{
    // now you *know* that it *IS* in fact a "GroupPrincipal"
    .....
}