Aufgabenstellung
In größeren Firmen kann über das LDAP-Protkoll auf Personendaten und -informationen zugegriffen werden. Die kann z.B. für eine Authentifizierung per Singlesignon genutzt werden.
Ansatz
Möchte man diesen Dienst nutzen, muss man zunächst wissen auf welchem Server im Unternehmen ein solcher Dienst angeboten wird. Dies könnte z.B. ein Active Directory Server (Windows Server) oder ein Lotus Notes Server (notesldap) sein.
Zunächst benötigt man den Import:
using System.DirectoryServices;
Lösung
private void button1_Click(object sender, EventArgs e) { DirectoryEntry DirEntry = new DirectoryEntry(); { DirEntry.Path = "LDAP://notesldap.it.company.com"; DirEntry.AuthenticationType = AuthenticationTypes.ServerBind; } Console.WriteLine(DirEntry.Name); DirectorySearcher search = new DirectorySearcher(DirEntry); { // Tweak this to refine your search search.Filter = "(sn=Karpenstein)"; // I limit my results while I tweak search.SearchScope = SearchScope.Subtree; } try { foreach (SearchResult result in search.FindAll()) { if (result != null) { DirectoryEntry de = result.GetDirectoryEntry(); textBox1.Text += "===================================="+br(); textBox1.Text += "User: \t\t" + de.Properties["uid"].Value.ToString() + br() + "objectclass:\t\t" + de.Properties["objectclass"].Value.ToString(); IDictionaryEnumerator ide = de.Properties.GetEnumerator(); ide.Reset(); try { while (ide.MoveNext()) { PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection; textBox1.Text += ide.Entry.Key.ToString(); textBox1.Text += pvc.Value; } } catch (Exception sdf) { textBox1.Text += sdf.Message; } //textBox1.Text += "location:\t\t" + de.Properties["location"]. Value.ToString() + br(); //textBox1.Text += "maildomain:\t\t" + de.Properties["maildomain"]. Value.ToString() + br(); textBox1.Text += "sn:\t\t" + de.Properties["sn"]. Value.ToString() + br(); //textBox1.Text += "dominocertificate:\t\t" + de.Properties["dominocertificate"]. Value.ToString() + br(); textBox1.Text += "mail:\t\t" + de.Properties["mail"]. Value.ToString() + br(); textBox1.Text += "l:\t\t" + de.Properties["l"]. Value.ToString() + br(); textBox1.Text += "displayname:\t\t" + de.Properties["displayname"]. Value.ToString() + br(); textBox1.Text += "givenname:\t\t" + de.Properties["givenname"]. Value.ToString() + br(); //textBox1.Text += "dc:\t\t" + de.Properties["dc"]. Value.ToString() + br(); //textBox1.Text += "dn:\t\t" + de.Properties["dn"]. Value.ToString() + br(); textBox1.Text += "cn:\t\t" + de.Properties["cn"]. Value.ToString() + br(); textBox1.Text += "===================================="; } } } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } finally { search.Dispose(); DirEntry.Close(); } }