开发者

Get users email addresses from Active Directory group

Hey, I need some simple test program which wi开发者_运维知识库ll allow me to set AD group name(e.g. testdomain.groupname) and based on this group name it retrieves me all the users(with subgroups as well) email addresses.

Any code snippet will be highly appreciated.

Thanks


If you're on .NET 3.5 (or you can upgrade to it), you can use the new System.DirectoryServices.AccountManagement namespace to make this quite easy.

Read more on that new .NET 3.5 gem here: 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 = "yournewemail@yourdomain.com";
         up.Save();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜