Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Email Address of an AD Security Group

In AD here at work, we have some security groups that are mail enabled. I am using the System.DirectoryServices.AccountManagement namespace like so:

        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);                        
                }                
            }
        }

Armed with this info, what's the correct way to find the group's email address?

like image 912
beaudetious Avatar asked Dec 15 '22 18:12

beaudetious


1 Answers

The AccountManagement libraries limit which properties you can access. If you want to get the email property for a group, you'll need to cast it back to a DirectoryEntry object.

PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"];
if (email.Value != null)
{
    // Do something with email property
}
like image 190
Cavyn VonDeylen Avatar answered Feb 03 '23 08:02

Cavyn VonDeylen