从ADGroup获取电子邮件地址列表

时间:2013-03-11 20:40:14

标签: c# active-directory

如果我有Active Directory组的名称,如何获取成员列表或其电子邮件地址?

2 个答案:

答案 0 :(得分:7)

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...
        }
    }
}

答案 1 :(得分:1)