Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public class LDAPService
  2. {
  3. private readonly string _rootStart;
  4. private readonly string _serverName;
  5.  
  6. public LDAPService(String RootStart, String ServerName)
  7. {
  8. _rootStart = RootStart;
  9. _serverName = ServerName;
  10. }
  11.  
  12. public bool Authenticate(string userName, string password)
  13. {
  14. bool authentic = false;
  15. try
  16. {
  17. DirectoryEntry entry = new DirectoryEntry("LDAP://" + _serverName, userName, password);
  18. object nativeObject = entry.NativeObject;
  19. authentic = true;
  20. }
  21. catch (DirectoryServicesCOMException)
  22. {
  23. //intentionally left empty
  24. }
  25. catch (System.Runtime.InteropServices.COMException ex)
  26. {
  27. throw new ApplicationException("The LDAP system is unavailable. Please inform the system administrator. (" + ex.Message + ")");
  28. }
  29. return authentic;
  30. }
  31.  
  32. public bool UserExists(string username, string password)
  33. {
  34. return GetUser(username, password) != null;
  35. }
  36.  
  37. public SearchResult GetUser(string username, string password)
  38. {
  39. SearchResult entry;
  40.  
  41. try
  42. {
  43. // create LDAP connection object
  44. DirectoryEntry myLdapConnection = createDirectoryEntry(username, password);
  45.  
  46. // create search object which operates on LDAP connection object
  47. // and set search object to only find the user specified
  48. DirectorySearcher search = new DirectorySearcher(myLdapConnection);
  49. //search.Filter = "(cn=" + username + ")";
  50. search.Filter = "(&(objectClass=User)(sAMAccountName=" + username + "))";
  51.  
  52. // create results objects from search object
  53. SearchResult result = search.FindOne();
  54. if (result != null)
  55. {
  56. // user exists, cycle through LDAP fields (cn, telephonenumber etc.)
  57. entry = result;
  58. }
  59. else
  60. {
  61. // user does not exist
  62. //Console.WriteLine("User not found!");
  63. entry = null;
  64. }
  65.  
  66. myLdapConnection.Close();
  67. myLdapConnection.Dispose();
  68.  
  69. // and finally...
  70. return entry;
  71. }
  72. catch (Exception e)
  73. {
  74. //Console.WriteLine("Exception caught:\n\n" + e.ToString());
  75. throw e;
  76. }
  77. finally
  78. {
  79.  
  80. }
  81. }
  82.  
  83. private DirectoryEntry createDirectoryEntry(string username, string password)
  84. {
  85. // create and return new LDAP connection with desired settings
  86.  
  87. DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://" + _serverName + "/" + _rootStart);
  88. ldapConnection.Username = username;
  89. ldapConnection.Password = password;
  90. ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
  91.  
  92. return ldapConnection;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement