Guest User

Untitled

a guest
Aug 17th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. LDAP access from dmz fails: : Information about the domain could not be retrieved (1355)
  2. using System;
  3.  
  4. public delegate void MessagingHandler(string message);
  5. public event MessagingHandler Messaged;
  6.  
  7. public Ldap35(string server, string adminuser, string adminpassword)
  8. {
  9. _ldapserver = server;
  10. _adminPassword = adminpassword;
  11. _adminUser = adminuser;
  12. }
  13. /// <summary>
  14. /// this will basically instantiate a UserPrincipal
  15. /// </summary>
  16. /// <param name="username">just the user</param>
  17. /// <param name="pass">just the password</param>
  18. /// <param name="domain">the correct domain, not sure if this is thedoamin.com or the_domain</param>
  19. /// <returns></returns>
  20. public bool Authenticate(string username, string pass, string domain)
  21. {
  22. if ( _connection == null)
  23. EstablishDirectoryConnection();
  24.  
  25. ValidateConnection();
  26. if (!domain.IsEmpty() && !username.Contains("\") && !username.Contains("/"))
  27. username = domain + "\" + username;
  28.  
  29. _userData = UserPrincipal.FindByIdentity(_connection, username);
  30.  
  31. if (_userData == null)
  32. throw new ApplicationException("Unable to locate user.");
  33.  
  34. if (! _connection.ValidateCredentials(username, pass))
  35. throw new ApplicationException("Invalid credentials. Unable to log in.");
  36.  
  37. //_userData = new UserPrincipal( _connection, username, pass, true );
  38.  
  39. return true;
  40. }
  41.  
  42. public bool Authenticate(string username, string pass)
  43. {
  44. return Authenticate(username, pass, "");
  45. }
  46.  
  47. public bool IsMemeberOfGroup(string group)
  48. {
  49. ValidateConnection(); ValidateUser();
  50. return _userData.IsMemberOf(new GroupPrincipal(_connection));
  51. }
  52.  
  53. public bool IsMemeberOfGroup(string group, bool caseSensitive)
  54. {
  55. if (caseSensitive)
  56. return IsMemeberOfGroup(group);
  57.  
  58. GetGroups();
  59.  
  60. return _groups.Any(g => g.ToLower().Trim() == group.ToLower().Trim());
  61.  
  62. }
  63.  
  64. public IList<string> GetGroups()
  65. {
  66. if (_groups == null)
  67. _groups = new List<string>();
  68.  
  69. ValidateConnection(); ValidateUser();
  70. Print("Getting groups");
  71. DirectoryEntry de = (DirectoryEntry)_userData.GetUnderlyingObject();
  72. object obGroups = de.Invoke("Groups");
  73. foreach (object ob in (IEnumerable)obGroups)
  74. {
  75. // Create object for each group.
  76.  
  77. var obGpEntry = new DirectoryEntry(ob);
  78. Print(obGpEntry.Name);
  79. _groups.Add(obGpEntry.Name);
  80. }
  81. return _groups;
  82. }
  83.  
  84. /// <summary>
  85. /// PrincipalContext class to establish a connection to the target directory and specify credentials for performing operations against the directory. This approach is similar to how you would go about establishing context with the DirectoryContext class in the ActiveDirectory namespace.
  86. /// </summary>
  87. /// <param name="adminuser">a user with permissions on the domain controller</param>
  88. /// <param name="adminpassword">the password to go with the above</param>
  89. /// <returns></returns>
  90. private void EstablishDirectoryConnection()
  91. {
  92. _connection = new PrincipalContext(ContextType.Domain, _ldapserver, "DC=thedomain,DC=com", ContextOptions.SimpleBind, _adminUser, _adminPassword);
  93. }
  94.  
  95. private void Print(string message)
  96. {
  97. if (Messaged != null)
  98. Messaged(message);
  99. }
  100.  
  101. private void ValidateConnection()
  102. {
  103. if ( _connection == null)
  104. throw new ApplicationException("No connection to server, please check credentials and configuration.");
  105. }
  106.  
  107. private void ValidateUser()
  108. {
  109. if (_userData == null)
  110. throw new ApplicationException("User is not authenticated. Please verify username and password.");
  111. }
  112.  
  113. public void Dispose()
  114. {
  115. _userData.Dispose();
  116. _connection.Dispose();
  117. }
  118. }
  119.  
  120. var _connection = new PrincipalContext(ContextType.Domain, _ldapserver, "DC=domain,DC=com", ContextOptions.SimpleBind, _adminUser, _adminPassword);
  121. var _userData = UserPrincipal.FindByIdentity(_connection, username);
  122.  
  123. DirectoryEntry de = (DirectoryEntry)_userData.GetUnderlyingObject();
  124. object obGroups = de.Invoke("Groups");
Add Comment
Please, Sign In to add comment