如果我有一个Active Directory组的名称,如何获得成员列表或他们的电子邮件地址?

最佳答案

void Main()
{
    string groupName = "somegroup";
    string domainName = "somedomain";

    using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName))
    {
        using(GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
        {
            var sams = from x in grp.GetMembers(true) select new {x.SamAccountName, };
            var users = from sam in sams.Distinct()
                let usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, sam.SamAccountName)
                select new { usr.SamAccountName, usr.DisplayName, usr.EmailAddress};
            //do something with users...
        }
    }
}

10-06 10:52