Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using System;
  2. using System.DirectoryServices;
  3.  
  4. namespace LDAPLes4
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.Write("Enter user: ");
  11. String username = Console.ReadLine();
  12.  
  13. try
  14. {
  15. // create LDAP connection object
  16. DirectoryEntry de = new DirectoryEntry("LDAP://ldap.itd.umich.edu", "", "", AuthenticationTypes.None);
  17.  
  18.  
  19. // create search object which operates on LDAP connection object
  20. // and set search object to only find the user specified
  21. //Console.WriteLine(de.ToString());
  22. DirectorySearcher search = new DirectorySearcher(de);
  23. Console.WriteLine("Test");
  24. search.Filter = "(cn=" + username + ")";
  25.  
  26. // create results objects from search object
  27.  
  28. SearchResult result = search.FindOne();
  29.  
  30. if (result != null)
  31. {
  32. // user exists, cycle through LDAP fields (cn, telephonenumber etc.)
  33.  
  34. ResultPropertyCollection fields = result.Properties;
  35.  
  36. foreach (String ldapField in fields.PropertyNames)
  37. {
  38. // cycle through objects in each field e.g. group membership
  39. // (for many fields there will only be one object such as name)
  40.  
  41. foreach (Object myCollection in fields[ldapField])
  42. Console.WriteLine(String.Format("{0,-20} : {1}",
  43. ldapField, myCollection.ToString()));
  44. }
  45. }
  46.  
  47. else
  48. {
  49. // user does not exist
  50. Console.WriteLine("User not found!");
  51. }
  52. }
  53.  
  54. catch (Exception e)
  55. {
  56. Console.WriteLine("Exception caught:\n\n" + e.ToString());
  57. }
  58.  
  59. Console.ReadKey();
  60. }
  61.  
  62.  
  63.  
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement