Active Directory User Group Memberships GroupPrincipal

前端 未结 2 1732
醉酒成梦
醉酒成梦 2021-01-05 10:19

I am trying to use GroupPrincipal (part of the System.DirectoryServices.AccountManagement namespace) to populate a list of type string, so I can ch

2条回答
  •  生来不讨喜
    2021-01-05 10:40

    This modification of your code works (I made tests to ensure):

    using System.DirectoryServices.AccountManagement;
    
    
        private static readonly string DomainName = "domaincontrollercomputer.domain.com";
        private static readonly string DomainContainer = "DC=DOMAIN,DC=COM";
        private static readonly string ADGroupName = "AD Group Name";
        private List GroupName {get;set;}
          
        private void populateGroups()
        {
            using (var ctx = new PrincipalContext(ContextType.Domain, DomainName, DomainContainer))
            {
                using (var grp = GroupPrincipal.FindByIdentity(ctx, ADGroupName))
                {
                    GroupName = new List();
                    foreach (var member in grp.GetMembers())
                    {
                        GroupName.Add(member.SamAccountName);
                    }
                }
            }
        }
    

提交回复
热议问题