SHARE
TWEET

Untitled

a guest Sep 3rd, 2018 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Getting the parent organization unit (if any) of an organizational unit in Active Directory
  2. public class OrganizationalUnit
  3. {
  4.     public string Name { get; set; }
  5.     public string ParentUO { get; set; }
  6.     public string Path { get; set; }    
  7. }
  8.    
  9. /// <summary>
  10.     /// Provides an object that allows you to get organizational units within an
  11.     /// active directory domain.
  12.     /// </summary>
  13.     /// <param name="connectionString">The LDAP connection string to a domain.
  14.     /// For example LDAP://DC=YourCompany,DC=com</param>
  15.     public ActiveDirectoryOrganizationalUnitRepository(string connectionString, string username, string password)
  16.     {
  17.         organizationalUnits = new List<OrganizationalUnit>();
  18.  
  19.         if (DomainExists(connectionString))
  20.         {
  21.             var baseDirectory = new DirectoryEntry(connectionString);
  22.             baseDirectory.Username = username;
  23.             baseDirectory.Password = password;
  24.  
  25.             DirectorySearcher searcher = new DirectorySearcher();
  26.             searcher.SearchRoot = baseDirectory;
  27.             searcher.Filter = "(objectCategory=organizationalUnit)";
  28.             searcher.SearchScope = SearchScope.Subtree;
  29.  
  30.             var ouResults = searcher.FindAll();
  31.  
  32.             foreach (SearchResult ou in ouResults)
  33.             {
  34.                 organizationalUnits.Add(new OrganizationalUnit() {
  35.                                         Path = ou.Path,
  36.                                         Name = ou.Properties["name"][0].ToString(),
  37.                                         ParentUO = ou.Properties["parent"][0].ToString()}
  38.                 );                    
  39.             }
  40.         }        
  41.     }
  42.    
  43. ParentUO = ou.Properties["parent"][0].ToString();
  44.    
  45. OU=currentOU,OU=parentOU,...,DC=domain,DC=..
  46.    
  47. String myADSPath = "LDAP://onecity/CN=user,CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
  48. DirectoryEntry myDirectoryEntry=new DirectoryEntry(myADSPath, UserName, SecurelyStoredPassword);
  49.  
  50. Console.WriteLine("Parent is :"+myDirectoryEntry.Parent.Path);
  51.    
  52. DirectorySearcher searcher = new DirectorySearcher();
  53.  
  54. searcher.SearchRoot = baseDirectory;
  55. searcher.Filter = "(objectCategory=organizationalUnit)";
  56. searcher.SearchScope = SearchScope.Subtree;
  57.  
  58. var ouResults = searcher.FindAll();
  59.  
  60. foreach (SearchResult ou in ouResults)
  61. {
  62.  
  63.     ResultPropertyCollection myResultPropColl;
  64.     myResultPropColl = ou.Properties;
  65.     Console.WriteLine("The properties of the " +
  66.             "'mySearchResult' are :");
  67.  
  68.     foreach (string myKey in myResultPropColl.PropertyNames)
  69.     {
  70.         string tab = "    ";
  71.         Console.WriteLine(myKey + " = ");
  72.         foreach (Object myCollection in myResultPropColl[myKey])
  73.         {
  74.             Console.WriteLine(tab + myCollection);
  75.         }
  76.     }
  77. }
  78.    
  79. organizationalUnits.Add(new OrganizationalUnit() {
  80.                                     Path = ou.Path,
  81.                                     Name = ou.Properties["name"][0].ToString(),
  82.                                     ParentUO = ou.GetDirectoryEntry().Parent.Path}
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