Pulling the users of an Active Directory groups
From Logic Wiki
using System;
using System.DirectoryServices.AccountManagement;
using tht.EDRMS.Service.Interfaces;
namespace tht.EDRMS.Service.Services
{
public class ActiveDirectoryService : IActiveDirectoryService
{
public string GetAdGroupMembers(string groupName)
{
var users = "";
using (var ctx = new PrincipalContext(ContextType.Domain, "tht.local"))
{
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName);
if (grp != null)
{
foreach (Principal p in grp.GetMembers(true))
{
UserPrincipal up = (p as UserPrincipal);
if (up != null)
{
users += up.EmailAddress + ", ";
}
}
grp.Dispose();
}
}
return users;
}
}
}