嘿, 我需要一些简单的测试程序,它允许我设置 AD 组名称(例如 testdomain.groupname),并根据该组名称检索所有用户(以及子组)的电子邮件地址。
任何代码片段都将受到高度赞赏。
谢谢
最佳答案
如果您使用的是 .NET 3.5(或者可以升级到它),则可以使用新的 System.DirectoryServices.AccountManagement
命名空间来简化此操作。
在此处了解有关新 .NET 3.5 gem 的更多信息: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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d5442585f43485a48404c44416d5442585f4942404c4443034e4240" rel="noreferrer noopener nofollow">[email protected]</a>";
up.Save();
}
}
关于.net - 从 Active Directory 组获取用户电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4025549/