Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6888771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:04:10+00:00 2026-05-27T06:04:10+00:00

I have an application that uses ActiveDirecotry authorisation and it has been decided that

  • 0

I have an application that uses ActiveDirecotry authorisation and it has been decided that it needs to support nested AD groups, e.g.:

MAIN_AD_GROUP
     |
     |-> SUB_GROUP
              | 
              |-> User

So, the user in not directly a member of MAIN_AD_GROUP. I’d like to be able to look for the user recursively, searching the groups nested in MAIN_AD_GROUP.

The main problem is that I’m using .NET 3.5 and there is a bug in System.DirectoryServices.AccountManagement in .NET 3.5 whereby the method UserPrincipal.IsMemberOf() will not work for groups with more than 1500 users. So I can’t use UserPrincipal.IsMemberOf() and no, I can’t switch to .NET 4 either.

I’ve worked around this last problem with the following function:

private bool IsMember(Principal userPrincipal, Principal groupPrincipal)
{
    using (var groups = userPrincipal.GetGroups())
    {
        var isMember = groups.Any(g => 
            g.DistinguishedName == groupPrincipal.DistinguishedName);
        return isMember;
    }
}

But userPrincipal.GetGroups() only returns the groups of which the user is a direct member.

How can I get this to work with nested groups?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:04:11+00:00Added an answer on May 27, 2026 at 6:04 am

    Workaround #1

    This bug is reported here at Microsoft Connect along with the following code that works around this issue by manually iterating through the PrincipalSearchResult<Principal> returned objects, catching this exception, and continuing on:

    PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
    var iterGroup = groups.GetEnumerator();
    using (iterGroup)
    {
        while (iterGroup.MoveNext())
        {
            try
            {
                Principal p = iterGroup.Current;
                Console.WriteLine(p.Name);
            }
            catch (NoMatchingPrincipalException pex)
            {
                continue;
            }
        }
    }
    

    Workaround #2

    Another workaround found here avoids the AccountManagement class, and uses the System.DirectoryServices API instead:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.DirectoryServices;  
    
    namespace GetGroupsForADUser  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                String username = "Gabriel";  
    
                List<string> userNestedMembership = new List<string>();  
    
                DirectoryEntry domainConnection = new DirectoryEntry(); // Use this to query the default domain
                //DirectoryEntry domainConnection = new DirectoryEntry("LDAP://example.com", "username", "password"); // Use this to query a remote domain
    
                DirectorySearcher samSearcher = new DirectorySearcher();  
    
                samSearcher.SearchRoot = domainConnection;  
                samSearcher.Filter = "(samAccountName=" + username + ")";  
                samSearcher.PropertiesToLoad.Add("displayName");  
    
                SearchResult samResult = samSearcher.FindOne();  
    
                if (samResult != null)  
                {  
                    DirectoryEntry theUser = samResult.GetDirectoryEntry();  
                    theUser.RefreshCache(new string[] { "tokenGroups" });  
    
                    foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])  
                    {  
                        System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);  
    
                        DirectorySearcher sidSearcher = new DirectorySearcher();  
    
                        sidSearcher.SearchRoot = domainConnection;  
                        sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";  
                        sidSearcher.PropertiesToLoad.Add("distinguishedName");  
    
                        SearchResult sidResult = sidSearcher.FindOne();  
    
                        if (sidResult != null)  
                        {  
                            userNestedMembership.Add((string)sidResult.Properties["distinguishedName"][0]);  
                        }  
                    }  
    
                    foreach (string myEntry in userNestedMembership)  
                    {  
                        Console.WriteLine(myEntry);  
                    }  
    
                }  
                else 
                {  
                    Console.WriteLine("The user doesn't exist");  
                }  
    
                Console.ReadKey();  
    
            }  
        }  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses the Authlogic gem and needs the functionality of
I have an application that uses the Ultra Win Grid and has a column
I have an application that uses a nested set model class to organise my
I have an application that uses a nested set model class to organise my
I have an application that uses a frame extensively and needs to hide/show certain
We have an ASP.NET application running at a customer site that uses ActiveDirectory for
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have client application that uses WCF service to insert some data to backend
I have an application that uses the accelerometer. Sometimes, the application will launch without
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.