SHARE
TWEET

Untitled

a guest Aug 18th, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. active directory findone() method
  2. DirectoryEntry de = null;
  3.             SearchResult results = null;
  4.             de = new DirectoryEntry();
  5.  
  6.             //geting the result FROM ad
  7.             de.Path = dr.manager;
  8.             de.AuthenticationType = AuthenticationTypes.Secure;
  9.             DirectorySearcher search = new DirectorySearcher(de);
  10.             search.Filter = string.Format("(objectClass={0})",'*');
  11.             search.PropertiesToLoad.Add("IsraelID");
  12.             results = search.FindOne();
  13.             de = results.GetDirectoryEntry();
  14.    
  15. System.Runtime.InteropServices.COMException (0x80004005): Unspecified error
  16.  
  17.    at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
  18.    at System.DirectoryServices.DirectoryEntry.Bind()
  19.    at System.DirectoryServices.DirectoryEntry.get_AdsObject()
  20.    at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
  21.    at System.DirectoryServices.DirectorySearcher.FindOne()
  22.    
  23. string LDAP = "LDAP://DC=MYDOMAIN,DC=COM";
  24. using (DirectoryEntry dirEntry = new DirectoryEntry(LDAP, null, null, AuthenticationTypes.Secure))
  25.     using (DirectorySearcher dirSearch = new DirectorySearcher(
  26.         dirEntry,
  27.         string.Concat("(objectClass=*)"),
  28.         new string[] { "IsraelID" }))
  29.     {
  30.         SearchResult result = dirSearch.FindOne();
  31.         if (result != null)
  32.             return result.Properties["IsraelID"][0].ToString();
  33.         else
  34.             return null;
  35.     }
  36.    
  37. DirectoryEntry de = null;
  38. SearchResult results = null;
  39. de = new DirectoryEntry();
  40.  
  41. // Assuming your domain dns name is treyresearch.net
  42. de.Path = "LDAP://servername/CN=users,DC=treyresearch,DC=net";
  43. de.AuthenticationType = AuthenticationTypes.Secure;
  44. de.Username = "treyresearch\Administrator";
  45. de.Password = "P@$$W0rd";
  46. DirectorySearcher search = new DirectorySearcher(de);
  47. search.Filter = string.Format("(objectClass={0})",'*');
  48. search.PropertiesToLoad.Add("IsraelID");
  49. results = search.FindOne();
  50. de = results.GetDirectoryEntry();
  51.    
  52. /* Connection to Active Directory
  53.  */
  54. DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr", "jpb", "Pwd");
  55. //DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr");
  56.  
  57. /* Directory Search
  58.  */
  59. DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
  60. dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
  61. dsLookForOUs.SearchScope = SearchScope.Subtree;
  62. dsLookForOUs.PropertiesToLoad.Add("cn");
  63. dsLookForOUs.PropertiesToLoad.Add("ou");
  64.  
  65. SearchResultCollection srcOUs = dsLookForOUs.FindAll();
  66.  
  67. foreach (SearchResult srOU in srcOUs)
  68. {
  69.   Console.WriteLine("{0}", srOU.Path);
  70.  
  71. }
  72.    
  73. de.path=dr.dr.manager
  74.    
  75. de.Path = "LDAP://"+dr.manager;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top