LDAP etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
LDAP etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

Reading Information from the LDAP Server with C#

Bring information down from the server and format it into an XmlDocument object. So let's add some variables to our method. I have initialized them to string.Empty and for the instance of AuthenticationTypes, I have initialized it to Anonymous.

string domainAndUsername = string.Empty;
string userName = string.Empty;
string passWord = string.Empty;
AuthenticationTypes at = AuthenticationTypes.Anonymous;


Next we populate the connectivity information with real values:

domainAndUsername = @"LDAP://123.12.12.123/dc=youcanlearnseries,dc=abc.us,dc=com";
userName= "kenno" ;
passWord= "password";
at = AuthenticationTypes.Secure;


Yes, we could have populated this information when we initialized the objects, but if you want to pass in multiple Directory servers, Active Directory, Sun One, etc., then this allows you to create the variable and then populate it later after user input.

Then we create an instance of a DirectoryEntry object. This object encapsulates a node in the Directory Services hierarchy. We pass into the object the connectivity information we created previously.

DirectoryEntry entry = new DirectoryEntry(domainAndUsername,userName, passWord,at);

To actually query the Directory server, we will create an instance of a DirectorySearcher object and we will pass into the DirectorySearcher object the DirectoryEntry object we created in the previous step.

DirectorySearcher mySearcher = new DirectorySearcher(entry);

The results of the query are stored in a collection object, SearchResultCollection, called results.

SearchResultCollection results;

We still want to filter the final results so we add the filter query to the mySearcher object.

mySearcher.Filter = filter;

And finally, we query the LDAP repository, storing the results in the results collection.

results = mySearcher.FindAll();

The rest of the formula is simple yet slightly confusing in its implementation. We have all the information returned for our query of the Directory Server stored in a SearchResultCollection object as a collection of properties and values. We are going to build our own XML object to return to our calling web service. Using a foreach loop, we iterate through the result collection, pulling out properties first and then within each property we find the actual value stored in the hierarchy. The code below has been shortened to show how the loop through the collection works.

foreach(SearchResult resEnt in results)
{
ResultPropertyCollection propcoll=resEnt.Properties;
foreach(string key in propcoll.PropertyNames)
{
foreach(object values in propcoll[key])
{ switch (key)
case "sn":
sb.Append(key.ToString() + ""
+ values.ToString() + "
");
break;
case "cn":
sb.Append(key.ToString() + ""
+ values.ToString() + "
");
break;
case "name":
sb.Append(key.ToString() + ""
+ values.ToString() + "
");
break;
}
}
}
}

The "key" that we need is determined by the LDAP directory you are using. Sun One uses different keys than Microsoft's Active Directory. So the system administrator might be able to inform you of the keys to use for your specific application, or you filter at a higher level and see what keys are returned and break it down yourself.

Each user that we select from our query we are going to wrap in a tag of our XML object. Notice that we create them as a StringBuilder object and then before we send them to the calling object, we load them into an XmlDocument object. We could have created an XML schema and then loaded them into the XmlDocument as we go, but for this example, I felt that was more information than needed.

Visual Studio 2017'de Devexpress 17.2.5 Toolbox görünmüyor

Visual Studio 2017 Toolbox'ı üzerinde Developer Express componentlerini göremiyorsanız aşağıdaki komutu çalıştırmak işini görecektir. (...