what i want to do is to retreive all the groups from an ldap path .... i am able to retreive a certain number of groups but i came to know that these are not all the groups....so i need a way to retreive all the groups including base,sub and all...how can
i do this?????
Similarly i am retrieving all the groups of a user where also i fera i am not retreiving all the groups ...
i am pasting both of the codes so please guide me if i am wrong.....
For all the groups in a path...........
public List<string> GetGroups()
{
SqlConnection conn_ldap = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database_OR.mdf;Integrated Security=True;User Instance=True");
conn_ldap.Open();
String Select_cmd_ldap = "select LDAP_Address,Access_ID,Access_Password from LDAPAddress_Table ";
SqlCommand cmd_ldap = new SqlCommand(Select_cmd_ldap, conn_ldap);
SqlDataAdapter DA_ldap = new SqlDataAdapter(cmd_ldap);
DataSet DS_ldap = new DataSet();
DA_ldap.Fill(DS_ldap);
conn_ldap.Close();
String Username = DS_ldap.Tables[0].Rows[0]["Access_ID"].ToString();
String Password = DS_ldap.Tables[0].Rows[0]["Access_Password"].ToString();
String Address = DS_ldap.Tables[0].Rows[0]["LDAP_Address"].ToString();
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Results collection.
string strPath = null;
// Binding path.
List<string> result = new List<string>();
// Construct the binding string.
strPath = Address;
//Change to your ADserver
// Get the AD LDS object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.Username = Username;
objADAM.Password = Password;
objADAM.RefreshCache();
}
catch (Exception e)
{
Nikhil3102
Member
18 Points
59 Posts
Cannot retreive all the groups from an ldap path and a user
Dec 23, 2010 04:00 PM|LINK
what i want to do is to retreive all the groups from an ldap path .... i am able to retreive a certain number of groups but i came to know that these are not all the groups....so i need a way to retreive all the groups including base,sub and all...how can i do this?????
Similarly i am retrieving all the groups of a user where also i fera i am not retreiving all the groups ...
i am pasting both of the codes so please guide me if i am wrong.....
For all the groups in a path...........
public List<string> GetGroups()
{
SqlConnection conn_ldap = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database_OR.mdf;Integrated Security=True;User Instance=True");
conn_ldap.Open();
String Select_cmd_ldap = "select LDAP_Address,Access_ID,Access_Password from LDAPAddress_Table ";
SqlCommand cmd_ldap = new SqlCommand(Select_cmd_ldap, conn_ldap);
SqlDataAdapter DA_ldap = new SqlDataAdapter(cmd_ldap);
DataSet DS_ldap = new DataSet();
DA_ldap.Fill(DS_ldap);
conn_ldap.Close();
String Username = DS_ldap.Tables[0].Rows[0]["Access_ID"].ToString();
String Password = DS_ldap.Tables[0].Rows[0]["Access_Password"].ToString();
String Address = DS_ldap.Tables[0].Rows[0]["LDAP_Address"].ToString();
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Results collection.
string strPath = null;
// Binding path.
List<string> result = new List<string>();
// Construct the binding string.
strPath = Address;
//Change to your ADserver
// Get the AD LDS object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.Username = Username;
objADAM.Password = Password;
objADAM.RefreshCache();
}
catch (Exception e)
{
}
// Get search object, specify filter and scope,
// perform search.
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = "(&(objectClass=group))";
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception e)
{
}
// Enumerate groups
try
{
if (objSearchResults.Count != 0)
{
// SearchResult objResult = default(SearchResult);
foreach (SearchResult objResult1 in objSearchResults)
{
objGroupEntry = objResult1.GetDirectoryEntry();
result.Add(objGroupEntry.Name);
}
}
else
{
// throw new Exception("No groups found");
}
}
catch (Exception e)
{
// throw new Exception(e.Message);
}
return result;
}
For groups of a user:
try
{
DirectoryEntry obEntry = new DirectoryEntry(Address);
obEntry.Username = Login_Xconnect.UserName;
obEntry.Password = Login_Xconnect.Password;
obEntry.RefreshCache();
DirectorySearcher srch = new DirectorySearcher(obEntry, "(sAMAccountName=" + strUser + ")");
// DirectorySearcher srch1= new DirectorySearcher(obEntry,
SearchResult res = srch.FindOne();
if (null != res)
{
DirectoryEntry obUser = new DirectoryEntry(res.Path);
obUser.Username = ID;
obUser.Password = Password;
object obGroups = obUser.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
// Create object for each group.
DirectoryEntry obGpEntry = new DirectoryEntry(ob);
obGpEntry.Username = ID;
obGpEntry.Password = Password;
groups.Add(obGpEntry.Name);
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return groups;
As i said there is no exception but it doesnt give all the groups so please let me know how else can i do this...
Thanks
Nikhil
smirnov
All-Star
23588 Points
4050 Posts
Re: Cannot retreive all the groups from an ldap path and a user
Feb 23, 2013 08:34 AM|LINK
You can try to use SizeLimit or PageSize or try to use enumerate as shown here
http://www.srinivasvangala.com/2011/03/get-all-ad-groups-using-ldap-in-c.html