Salahuddin Ahmed
Salahuddin Ahmed

Reputation: 5640

.NET Core - Novell LDAP/AD - Group search for a user that he belongs to - Has anybody made it work?

My code given below is working well while searching groups for a user, but the problem is it returns only one group. My goal is to get all groups the user belongs to. How can I get rid of this problem? Any help will be much appreciated.

LdapSearchResults lsc = (LdapSearchResults)ldapCon.Search(                    
    "DC=adl,DC=local",                   
    LdapConnection.ScopeSub,                    
    "(sAMAccountName=" + Username + ")",
    null,
    false
);

while (lsc.HasMore())
{                        
    try
    {
        var nextEntry = lsc.Next();                            
        nextEntry.GetAttributeSet();                           

        adGroups.Add(new ADUserSecurityGroupModel { 
            member = nextEntry.GetAttribute("memberOf").StringValue,
            distinguishedName = nextEntry.GetAttribute("sAMAccountName").StringValue 
        });
    }
    catch (LdapException ex)
    {
        Console.WriteLine("Error: " + ex.ToString());
        continue;
    }
}

Upvotes: 1

Views: 2766

Answers (1)

Salahuddin Ahmed
Salahuddin Ahmed

Reputation: 5640

After some research and study finally I have got a solution regarding the problem posted here. This workaround is enough to meet the requirement.

LdapSearchResults lsc = (LdapSearchResults)ldapCon.Search(
OU=Dashboards,DC=adl,DC=local",
LdapConnection.ScopeSub,
"(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=CN=" + UserFullName + 
",OU=Company Name,DC=adl,DC=local))",
null,
false);                

while (lsc.HasMore())  
 {
  LdapEntry nextEntry = null;
  try
    {
      nextEntry = lsc.Next();
    }
  catch
    {                            
      continue;
    }
  nextEntry.GetAttributeSet();
  adGroups.Add(new ADUserSecurityGroupModel { cn = 
  nextEntry.GetAttribute("cn").StringValue });
 };

Upvotes: 1

Related Questions