如何使用.net 3.5或4中的DirectoryServices.AccountManagement在Active Directory中创建新OU

dan*_*ael 6 .net ldap .net-4.0 active-directory .net-3.5

在Active Directory中创建和查找用户和组我一直在使用此代码:http: //anyrest.wordpress.com/2010/06/28/active-directory-c/ 正在使用新的System.DirectoryServices.AccountManagement在.net 3.5中引入的命名空间...

我想添加一个方法,使用.net 3.5或4.0的最新技术(而不使用旧的System.DirectoryServices)创建一个新的OU(如果OU不存在)

任何想法如何做到这一点?

JPB*_*anc 10

根据.NET Framework 3.5中的管理目录安全主体特别是此处的体系结构和System.DirectoryServices.AccountManagement命名空间文章,accountManagement适用于用户组和计算机(安全主体).

Active Directory架构

对于organizationalUnit,你可以System.DirectoryServices.ActiveDirectory在这里使用一个例子:

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();
}
Run Code Online (Sandbox Code Playgroud)

不要忘记使用using(){}指令