Получить адреса электронной почты пользователей из группы Active Directory

Эй, мне нужна простая тестовая программа, которая позволит мне установить имя группы AD (например, testdomain.groupname) и на основе этого имени группы получить мне адреса электронной почты всех пользователей (с подгруппами).

Любой фрагмент кода будет высоко оценен.

Спасибо


person Hrayr    schedule 26.10.2010    source источник


Ответы (1)


Если вы используете .NET 3.5 (или можете перейти на нее), вы можете использовать новое пространство имен System.DirectoryServices.AccountManagement, чтобы упростить эту задачу.

Подробнее об этой новой жемчужине .NET 3.5 читайте здесь: Управление принципами безопасности каталогов в .NET Платформа 3.5

// create a context - you need to supply your 
// domain NetBIOS-style, e.g. "CONTOSO"
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// find the group you're interested in
GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, "YourGroupName");

// enumerate the members of that group
// the "true" in GetMembers() means: enumerate recursively, e.g.
// if another group is found as member, its members are enumerated
PrincipalSearchResult<Principal> members = gp.GetMembers(true);

// iterate over the principals found
foreach(Principal p in members)
{
    // test to see if it's a UserPrincipal
    UserPrincipal up = (p as UserPrincipal);

    if (up != null)
    {
         // if it is - set the new e-mail address
         up.EmailAddress = "[email protected]";
         up.Save();
    }
}
person marc_s    schedule 26.10.2010