めも

技術メモとその他

ldap with c# 2


search way

1)with protocol
SearchRequest findme = new SearchRequest();
findme.DistinguishedName = "ou=People,dc=example,dc=com"; //Find all People in this ou
findme.Filter = "(objectClass=person)"; //The type of entry we are looking for
findme.Scope = System.DirectoryServices.Protocols.SearchScope.Subtree; //We want all
//entries below this ou
SearchResponse results = (SearchResponse)ldapConn.SendRequest(findme); //Run the query
//and get results
SearchResultEntryCollection entries = results.Entries;
for (int i = 0; i < entries.Count; i++)//Iterate through the results
{
SearchResultEntry entry = entries[i];
IDictionaryEnumerator attribEnum = entry.Attributes.GetEnumerator();
while (attribEnum.MoveNext())//Iterate through the result attributes
{
//Attributes have one or more values so we iterate through all the values
//for each attribute
DirectoryAttribute subAttrib = (DirectoryAttribute)attribEnum.Value;
for (int ic = 0; ic < subAttrib.Count; ic++)
{
//Attribute Name below
attribEnum.Key.ToString();
//Attribute Sub Value below
subAttrib[ic].ToString();
}
}
}

2)
DirectorySearcher search = new DirectorySearcher(dirEntry);
search.Filter = "(objectClass=person)";
search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
SearchResultCollection searchResults = search.FindAll();
for (int i = 0; i < searchResults.Count; i++)
{

System.Collections.IDictionaryEnumerator subColl =
searchResults[i].Properties.GetEnumerator();
while (subColl.MoveNext())
{
ResultPropertyValueCollection pc = (ResultPropertyValueCollection)subColl.Value;
System.Collections.IEnumerator subPcol = pc.GetEnumerator();
while (subPcol.MoveNext())
{
//Property Name
subColl.Key.ToString();
//Property Value
subPcol.Current.ToString();
}
}
}