2010-10-26 5 views
2

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

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

Благодаря

ответ

2

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

Подробнее о том новом .NET 3.5 жемчужину здесь: Managing Directory Security Principals in the .NET Framework 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(); 
    } 
} 
+0

Большое спасибо! Он отлично работает! – Hrayr

Смежные вопросы