如何获取所有Windows组?

发布于 2024-09-29 00:25:31 字数 604 浏览 5 评论 0原文

我写这个是为了获取特定用户所属的组:

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry user = AD.Children.Find(completeUserName, "user");
object obGroups = AD.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
   // Create object for each group.
    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
    listOfMyWindowsGroups.Add(obGpEntry.Name);
}
for (int j = 0; j < listOfMyWindowsGroups.Count; j++)
{
   //ex
}

How is it possible toretrieve all the groups in windows while not just for a certain user?

I wrote this to get the groups a particular user belongs to:

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry user = AD.Children.Find(completeUserName, "user");
object obGroups = AD.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
   // Create object for each group.
    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
    listOfMyWindowsGroups.Add(obGpEntry.Name);
}
for (int j = 0; j < listOfMyWindowsGroups.Count; j++)
{
   //ex
}

How is it possible to retrieve all the groups in windows and not just for a particular user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-10-06 00:25:31

如何为组设置过滤器并枚举结果?

试试这个过滤器:

AD.Children.SchemaFilter.Add("group");

How about setting up a filter for groups and enumerating the results.

Try this filter:

AD.Children.SchemaFilter.Add("group");
仅一夜美梦 2024-10-06 00:25:31

尝试一下这个,它会为您提供指定 OU 中的所有组。

public ArrayList GetGroups()
{
    ArrayList myItems = new ArrayList();

    // Create the principal context for the group object.
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    // Create the GroupPrincipal object and set the diplay name property. 
    GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);

    // Create a PrincipalSearcher object.     
    PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oGroupPrincipal);

    // Searches for all groups named "Administrators".
    PrincipalSearchResult<Principal> oPrincipalSearchResult = oPrincipalSearcher.FindAll();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

如需完整参考,您可以查看

.Net 3.5 版本 - > http://anyrest.wordpress.com/2010/06/28/ active-directory-c/

旧版本 - > http://anyrest.wordpress.com/2010/ 02/01/active-directory-objects-and-c/

Try this one out, it will give you all groups in a specicied OU.

public ArrayList GetGroups()
{
    ArrayList myItems = new ArrayList();

    // Create the principal context for the group object.
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    // Create the GroupPrincipal object and set the diplay name property. 
    GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);

    // Create a PrincipalSearcher object.     
    PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oGroupPrincipal);

    // Searches for all groups named "Administrators".
    PrincipalSearchResult<Principal> oPrincipalSearchResult = oPrincipalSearcher.FindAll();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

For a full reference you can check this one out

.Net 3.5 version - > http://anyrest.wordpress.com/2010/06/28/active-directory-c/

Older versions - > http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文