Search code examples
c#active-directoryldapactive-directory-group

Get all groups for a user using LDAP


I've tried to load all groups for a user from LDAP.

Currently I'm testing on our local AD. With the following code I can load all groups of the given user:

public IEnumerable<String> GetUserGroups( String userName )
{ 
    using ( var domainContext = new PrincipalContext( ContextType.Domain, Name ) )
    {
        var user = UserPrincipal.FindByIdentity( domainContext, userName );
        return user.GetAuthorizationGroups().Select( x => x.Name} ).ToList();
    }
}

But I fail to get the same result using LDAP.

Code using LDAP:

public IEnumerable<String> GetUserGroups1(String userName)
{
    //returns the container name of the given user
    var containerName = GetUserContainerName(userName); 
    var groups = new List<String>();
    if (containerName == null)
        return groups;

    var entry = new DirectoryEntry(String.Format("LDAP://{0}", "DC=example,DC=com"));

    var searcher = new DirectorySearcher(entry)
    {
        Filter = String.Format("(member:{0}:=CN={1},{2},{3})",
                               "1.2.840.113556.1.4.1941",
                               containerName, "CN=Users", "DC=example,DC=com"),
        SearchScope = SearchScope.Subtree
    };

    var result = searcher.FindAll();
    for (var i = 0; i < result.Count; i++)
    {
        var path = result[i].Path;
        var startIndex = path.IndexOf("CN=", StringComparison.Ordinal) + 3;
        groups.Add(path.Substring(startIndex, path.IndexOf(",", startIndex + 1,
                   StringComparison.Ordinal) - startIndex));
     }
     return groups;
}

How can I get all groups for a user using LDAP?


Solution

  • I’ve ended up with this code. It returns the name of each group containing a given user.

    private IEnumerable<String> GetGroupsOfUser( String userName )
    {
        var groupNames = new List<String>();
    
        // Open a LDAP connection
        using ( var ldapConnection = OpenLdapConnection() )
        {
            // Configuration (should work for an AD with default settings):
            // MemberOfAttributeKey => "memberOf"
            // UserFilterDn => "DC=domain1,DC=domain2,DC=domain3"
            // UserFilter => "(&(objectCategory=person)(sAMAccountName={0}))"
            // ProtocolVersion => 3
    
    
            // Search for the user data in the directory
            var ldapFilter = SecurityConfiguration.LdapConfiguration.UserFilter.F( userName );
            String[] attributesToReturn = { SecurityConfiguration.LdapConfiguration.MemberOfAttributeKey };
            var searchRequest = new SearchRequest( SecurityConfiguration.LdapConfiguration.UserFilterDn,
                                                    ldapFilter,
                                                    SearchScope.Subtree,
                                                    attributesToReturn );
    
            // Check if the response is valid
            var searchResponse = ldapConnection.SendRequest( searchRequest ) as SearchResponse;
            if ( searchResponse?.Entries?.Count != 1 )
                throw new DirectoryException( "Invalid search response received from the directory." );
    
            var entry = searchResponse.Entries[0];
            if ( !entry.Attributes.Contains( SecurityConfiguration.LdapConfiguration.MemberOfAttributeKey ) )
                Logger.Warn( "Entry does not contain a member of attribute." );
            else
                for ( var index = 0; index < entry.Attributes[SecurityConfiguration.LdapConfiguration.MemberOfAttributeKey]
                                                    .Count; index++ )
                {
                    // Extract the group name
                    var groupName = entry.Attributes[SecurityConfiguration.LdapConfiguration.MemberOfAttributeKey][index]
                                            .ToString();
                    var name = groupName.Substring( 3, Math.Min( groupName.IndexOf( ",", StringComparison.InvariantCultureIgnoreCase ) - 3, groupName.Length - 3 ) );
                    groupNames.Add( name );
                }
        }
    
        return groupNames;
    }
    
    private LdapConnection OpenLdapConnection()
    {
        // Use the server name and port to setup an LDAP Directory Service
        var directoryIdentifier = new LdapDirectoryIdentifier( SecurityConfiguration.LdapConfiguration.Server, SecurityConfiguration.LdapConfiguration.Port );
        var ldapConnection = new LdapConnection( directoryIdentifier );
    
        // Set the protocol version
        ldapConnection.SessionOptions.ProtocolVersion = SecurityConfiguration.LdapConfiguration.ProtocolVersion;
    
        // If user name parameter present set connection credentials
        if ( SecurityConfiguration.LdapConfiguration.UserName.IsNotEmpty() )
        {
            // Set connection credentials
            var networkCredential = new NetworkCredential( SecurityConfiguration.LdapConfiguration.UserName,
                                                            SecurityConfiguration.LdapConfiguration.Password );
    
            if ( SecurityConfiguration.LdapConfiguration.UserDomain.IsNotEmpty() )
                networkCredential.Domain = SecurityConfiguration.LdapConfiguration.UserDomain;
            ldapConnection.Credential = networkCredential;
    
            // Set connection authentication type
            ldapConnection.AuthType = SecurityConfiguration.LdapConfiguration.AuthType;
        }
    
        // Connection establishment
        ldapConnection.Bind();
    
        return ldapConnection;
    }