Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.DirectoryServices;
- namespace LDAPLes4
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Enter user: ");
- String username = Console.ReadLine();
- try
- {
- // create LDAP connection object
- DirectoryEntry de = new DirectoryEntry("LDAP://ldap.itd.umich.edu", "", "", AuthenticationTypes.None);
- // create search object which operates on LDAP connection object
- // and set search object to only find the user specified
- //Console.WriteLine(de.ToString());
- DirectorySearcher search = new DirectorySearcher(de);
- Console.WriteLine("Test");
- search.Filter = "(cn=" + username + ")";
- // create results objects from search object
- SearchResult result = search.FindOne();
- if (result != null)
- {
- // user exists, cycle through LDAP fields (cn, telephonenumber etc.)
- ResultPropertyCollection fields = result.Properties;
- foreach (String ldapField in fields.PropertyNames)
- {
- // cycle through objects in each field e.g. group membership
- // (for many fields there will only be one object such as name)
- foreach (Object myCollection in fields[ldapField])
- Console.WriteLine(String.Format("{0,-20} : {1}",
- ldapField, myCollection.ToString()));
- }
- }
- else
- {
- // user does not exist
- Console.WriteLine("User not found!");
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception caught:\n\n" + e.ToString());
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement