Filter results from GetAuthorizationGroups by Security Group Membership

I have a method to return a list string of all security groups of a given AD user. However I need to filter the returned list string to only show the groups which are a member of another given security group.

I Have provided the list string method, so what I now need to do is filter the returned userRoles (In a efficient manner ) and show only the groups that are a member of (lets say) "MyOtherSecurityGroup". Thanks

public List<string> test()
        {
            //ArrayList results = new ArrayList();
            List<string> userRoles = new List<string>();


            PrincipalContext context = new
            PrincipalContext(ContextType.Domain, "mydc", "DC=MYDOMAIN,DC=COM"
            , "MYDOMAIN\\user", "password");
            UserPrincipal p =
            UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "usertosearch");

            var groups = p.GetAuthorizationGroups();

            foreach (var group in groups)
            {
                userRoles.Add(group.Name);
            }
            return userRoles;
        }

Open in new window

mcourtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
GetAuthorizationGroups returns a collection of principal objects (GroupPrincipal) that contains all the authorization groups of which this user is a member. You will have to loop through each of these groups and call the GroupPrincipal.IsMemberOf method.

See my example.
List<string> userRoles = new List<string>();

PrincipalContext context = new
PrincipalContext(ContextType.Domain, "mydc", "DC=MYDOMAIN,DC=COM", "MYDOMAIN\\user", "password");

UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "usertosearch");

GroupPrincipal otherGroup = GroupPrincipal.FindByIdentity(context, "MyOtherSecurityGroup");

PrincipalSearchResult groups = p.GetAuthorizationGroups();

foreach (GroupPrincipal group in groups)
{
      if(group.IsMemberOf(otherGroup))
            userRoles.Add(group.Name);
}
return userRoles;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcourtAuthor Commented:
I came up with the following but looking at your example, I had overlooked the IsMemberOf, so I will award the points, thanks.
public List<string> getUserGroups()
        {
            //ArrayList results = new ArrayList();
            List<string> userRoles = new List<string>();


            PrincipalContext context = new
            PrincipalContext(ContextType.Domain, "DC", "DC=DOMAIN,DC=COM"
            , "domain\\user", "password");
            UserPrincipal p =
            UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "usertogetgroups");

            var groups = p.GetAuthorizationGroups();

            foreach (var group in groups)
            {
                if (CheckIfGroupInGroup(group.Name) == true)
                {
                userRoles.Add(group.Name);
                }
            }
            return userRoles;
        }

        public static bool CheckIfGroupInGroup(string groupName)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DC", "DC=DOMAIN,DC=COM", "domain\\user", "password");
            GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "GroupToCheck");

            bool result = false;

            if (grp != null)
            {
                foreach (Principal p in grp.GetMembers())
                {
                    if (p.Name == groupName)
                    {
                        result = true;
                    }
                }


                grp.Dispose();
                ctx.Dispose();

            }
            else
            {
                result = false;
            }
            return result;
        }

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.