.net - 从 Active Directory 组获取用户电子邮件地址

标签 .net active-directory

嘿, 我需要一些简单的测试程序,它允许我设置 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/

相关文章:

c# - 当类具有静态构造函数时,稍后初始化静态字段

active-directory - Active Directory作为OpenID提供程序?

powershell - 如何仅从 Active Directory 中检索已启用的用户

active-directory - IdentityServer4 使用 Active Directory 作为 SSO 的身份提供者

active-directory - 微软事件目录端口

ios - Visual Studio 中没有 iphone 设备

c# - 为 datagridview 单元格赋值

c# - 实例化泛型类时如何避免指定冗余类型

c# - document.readyState == "complete"始终为假。状态始终为 "interactive"

c# - 如何检测我的程序是否在 Active Directory 环境中运行?