How to get ADGroup type security and distribution both adgroup types. How to get the AdGroup type both security and distribution ? Cant see any property to distinguish the group type ?

GetGroups - Returns a collection of group objects that specify the groups of which the current principal is a member. This overloaded method only returns the groups of which the principal is directly a member; no recursive searches are performed.

GetAuthorizationGroups - Returns a collection of principal objects that contains all the authorization groups of which this user is a member. This function only returns groups that are security groups; distribution groups are not returned.

    public async Task<List<AdGroup>> GetGroups(string userName)
    {
       List<AdGroup> result = new List<AdGroup>();

        // establish domain context
        PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

        // find your user
        UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);

        // if found - grab its groups
        if (user != null)
        {
             PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();

            

            // PrincipalSearchResult<Principal> groups = user.GetGroups();
            var  i= 1;
            // iterate over all groups
            foreach (Principal p in groups)
            {
                // make sure to add only group principals
                if (p is GroupPrincipal)
                {                     
                    result.Add(new AdGroup
                    {
                        Id = i,
                        Name = ((GroupPrincipal)p).Name,
                       Type= ((GroupPrincipal)p).IsSecurityGroup.Value) ;

                    });
                    i++;
            }                                                     
            }
        }

        return result;
    }
0

There are 0 best solutions below