C# 使用C检查当前容器中是否存在Active Directory组#

C# 使用C检查当前容器中是否存在Active Directory组#,c#,active-directory,C#,Active Directory,我想创建一个新的Active Directory组 这是我的代码: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, container, userName, password); GroupPrincipal oGroupPrincipal = new GroupPrincipal(ctx, userName); DirectoryEntry entry = new DirectoryEntry("

我想创建一个新的Active Directory组

这是我的代码:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, container, userName, password);

GroupPrincipal oGroupPrincipal = new GroupPrincipal(ctx, userName);
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password,AuthenticationTypes.Secure);

if (entry.Children.Find("CN=" + groupName) != null) {

}

if (!DirectoryEntry.Exists("LDAP://" + System.Configuration.ConfigurationManager.AppSettings["Domain"] + "/CN=" + groupName + "," + System.Configuration.ConfigurationManager.AppSettings["Container"]))
{

     oGroupPrincipal.Description = groupName;
     oGroupPrincipal.GroupScope = (System.DirectoryServices.AccountManagement.GroupScope)Enum.Parse(typeof(System.DirectoryServices.AccountManagement.GroupScope), groupScope);
     oGroupPrincipal.IsSecurityGroup = isSecurity;
     oGroupPrincipal.Save(ctx);
}
我遇到的问题是,在创建新创建的组之前,先看看它是否存在。在此阶段,我的代码返回所有组都存在,因此我无法创建组

这是为了检查组是否存在:

if (entry.Children.Find("CN=" + groupName) != null) {

}
但它给出了一个例外,服务器上没有这样的对象

如果有任何帮助,我们将不胜感激。

您似乎(错误地)认为
条目.Children.Find()
将对整个目录进行递归搜索,而不是这样做

因此,您需要绑定到该组应位于的实际容器,然后检查其直接子级是否存在您的组:

DirectoryEntry entry = new DirectoryEntry("LDAP://YourServer/OU=SubOU,OU=TopLevelOU,dc=test,dc=com", userName, password,AuthenticationTypes.Secure);

try
{     
     DirectoryEntry childGroup = entry.Children.Find("CN=TestGroup2");
     // create group here
}
catch (DirectoryServicesCOMException exception)
{
    // handle the "child not found" case here ...
}
或者,您需要对您的组执行目录搜索,该搜索在整个目录中递归运行(因此速度也将大大降低):

//定义根目录项
DirectoryEntry domainRoot=新的DirectoryEntry(“LDAP://”+域、用户名、密码、AuthenticationTypes.Secure);
//设置子树搜索器和组搜索器
DirectorySearcher ds=新的DirectorySearcher(domainRoot);
ds.SearchScope=SearchScope.SubTree;
ds.Filter=“(&(cn=TestGroup2)(objectCategory=group))”;
var results=ds.FindAll();
如果(results.Count)创建
}

你能告诉我们这些配置设置有效吗?你有什么价值,例如在
groupName
中,你的组真的位于你的域的顶端吗?你需要绑定到该组应该存在的容器,例如像
LDAP://yourserver/OU=someOU,OU=topLevelOU,dc=test,dc=com
之类的东西不能只绑定到域的顶级,然后执行
entry.Children.Find()
-这不会递归遍历您的整个目录!这是我的应用程序设置:groupName是我要创建的新组的名称。在这种情况下,是TestGroup2I使用了搜索代码,效果很好,但现在当我创建组时,它没有在正确的容器中创建组。我缺少什么?我弄明白了,我已经更改了我的domainRoot:DirectoryEntry domainRoot=new DirectoryEntry(“LDAP://”+域+“/”+容器、用户名、密码、AuthenticationTypes.Secure);很抱歉再次打扰您,我如何才能使用它属性来更改GroupScope和组的安全性?group.properties[“sAmAccountName”].Value=groupName;谢谢,用于Children.Find的代码片段很有帮助,但当我使用它时,它会为不存在的子项引发异常,而不是像代码所期望的那样返回null。这里有一个参考,如果我有误解,请纠正我:
// define root directory entry
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + domain, userName, password,AuthenticationTypes.Secure);

// setup searcher for subtree and search for groups 
DirectorySearcher ds = new DirectorySearcher(domainRoot);
ds.SearchScope = SearchScope.SubTree;
ds.Filter = "(&(cn=TestGroup2)(objectCategory=group))";

var results = ds.FindAll();

if(results.Count <= 0)
{
   // group not found -> create
}