确定AD安全组的电子邮件地址

bea*_*ous 4 c# directoryservices active-directory

在AD工作中,我们有一些启用了邮件的安全组.我正在使用System.DirectoryServices.AccountManagement命名空间,如下所示:

        List<GroupPrincipal> result = new List<GroupPrincipal>();            
        using (PrincipalContext domain = new PrincipalContext(ContextType.Domain, userinfo[0]))
        using (UserPrincipal user = UserPrincipal.FindByIdentity(domain, username))
        {

            if (user != null)
            {
                PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();

                int totalGroupCounter = 0;
                StringBuilder output = new StringBuilder();
                List<GroupPrincipal> securityGroups = new List<GroupPrincipal>();
                List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>();

                foreach (Principal group in groups)
                {
                    totalGroupCounter++;

                    if (((GroupPrincipal)group).IsSecurityGroup.Value)                        
                        securityGroups.Add((GroupPrincipal)group);                        
                    else                        
                        distributionGroups.Add((GroupPrincipal)group);                        
                }                
            }
        }
Run Code Online (Sandbox Code Playgroud)

有了这些信息,找到该组的电子邮件地址的正确方法是什么?

Cav*_*len 12

AccountManagement库限制您可以访问的属性.如果要获取组的电子邮件属性,则需要将其强制转换为DirectoryEntry对象.

PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"];
if (email.Value != null)
{
    // Do something with email property
}
Run Code Online (Sandbox Code Playgroud)