Active Directory nested groups

前端 未结 2 723
自闭症患者
自闭症患者 2021-01-02 07:17

I have a C# 4.0 program working that retrieves all the members for a specific AD group. In this AD group are other AD groups containing other members. I need my program to i

相关标签:
2条回答
  • 2021-01-02 07:33

    Since you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context and easily find users and/or groups in AD. Also: the GroupPrincipal has a method called GetMembers which will list all members of that group - optionally, it will do so recursively for you!

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find the group you're interested in
    GroupPrincipal myGroup = GroupPrincipal.FindByIdentity(ctx, "SomeGroup");
    
    // if you found it - get its members
    if (myGroup != null)
    {
       // if your call the GetMembers, you can optionally specify a "Recursive" flag - done here
       var allMembers = myGroup.GetMembers(true);
    }
    

    The new S.DS.AM makes it really easy to play around with users and groups in AD!

    0 讨论(0)
  • 2021-01-02 07:33

    Assuming you're using the LDAP view into ActiveDirectory, the attribute you're looking for is called "objectClass". A group shows up with an objectClass of "groupOfNames", I believe; possibly "group". Alternatively, just look to see if the object has any "member"s, regardless of object class, and if it does, assume it's some sort of group and recurse.

    0 讨论(0)
提交回复
热议问题