How to create a new organizational unit in Active Directory using DirectoryServices.AccountManagement in .net 3.5 or 4

I used this code to create and search users and groups in Active Directory: http://anyrest.wordpress.com/2010/06/28/active-directory-c/ which uses the new System.DirectoryServices.AccountManagement namespace, which was Introduced in .net 3.5 ...

I would like to add a method that creates a new organizational unit (if the OU no longer exists) using the latest technology with .net 3.5 or 4.0 (and not using the old System.DirectoryServices services)

any idea how to do this?

+6
source share
1 answer

In accordance with "Managing Directory Security Principles in the .NET Framework 3.5," specializing in the architecture below and System.DirectoryServices.AccountManagement Namespace article, accountManagement is intended for groups of users and computers (security principals).

Active Directory Architecture

For organizationalUnit you can use System.DirectoryServices.ActiveDirectory , here is an example:

 using System.DirectoryServices; ... /* Connection to Active Directory */ DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/ou=Monou,dc=dom,dc=fr", "jpb", "PWD"); DirectorySearcher ouSrc = new DirectorySearcher(deBase); ouSrc.Filter = "(OU=TheNewOU)"; ouSrc.SearchScope = SearchScope.Subtree; SearchResult srOU = ouSrc.FindOne(); if (srOU == null) { /* OU Creation */ DirectoryEntry anOU = deBase.Children.Add("OU=TheNewOU", "organizationalUnit"); anOU.Properties["description"].Value = "The description you want"; anOU.CommitChanges(); } 

Remember to use the using(){} directive

+9
source

Source: https://habr.com/ru/post/899447/


All Articles