Reputation: 113
I am trying to retrieve all groups assigned to a user in LDAP Apache Directory Studio. I am using Novell.Directory.Ldap library and used .NET Core. I am not sure if I'm doing the correct one but, I have tried one below but it's giving me error on LdapAttribute attribute = userEntry.GetAttribute("memberOf");
{"The given key 'memberOf' was not present in the dictionary."}
. What I am doing wrong? Is there any other solution to achieve to retrieve all groups assigned to a user?
using (var connection = new LdapConnection() { SecureSocketLayer = false })
{
connection.Connect(_ldapSettings.Server, _ldapSettings.Port);
if (_ldapSettings.UseSSL)
connection.StartTls();
connection.Bind("cn=sample,ou=users,ou=system", password);
ILdapSearchResults searchResults = connection.Search(
"cn=sample,ou=users,ou=system",
LdapConnection.ScopeSub,
"(objectClass=*)",
null,
false
);
LdapEntry userEntry = searchResults.Next();
LdapAttribute attribute = userEntry.GetAttribute("memberOf");
if (attribute != null)
{
string[] groupDns = attribute.StringValueArray;
foreach (string groupDn in groupDns)
{
Console.WriteLine(groupDn);
}
}
return true;
}
Below is an example user assigned to group Administrators.
Upvotes: 3
Views: 1165
Reputation: 174730
You need to specify the attributes you want the LDAP server to return:
string[] attributeList = new string[] { "memberOf" };
ILdapSearchResults searchResults = connection.Search(
"cn=sample,ou=users,ou=system",
LdapConnection.ScopeSub,
"(objectClass=*)",
attributeList,
false
);
Upvotes: 0