如果我在Windows命令行上运行“net group /domain”,我会得到当前域服务器的“全局组”列表。我想从我的.NET代码中获取相同的列表。
通过谷歌搜索,我找到了成千上万的“如何获取用户所属的组”,“如何获取属于组的用户”等文章。我只想要所有全局组的列表;而不是与特定用户相关联的组。能否有人向我展示此代码,或者引用可以导出该代码的参考资料?
以下代码将查找当前域中的所有全局组:
using (var context = new PrincipalContext(ContextType.Domain))
using (var filter = new GroupPrincipal(context) { GroupScope = GroupScope.Global })
using (var searcher = new PrincipalSearcher(filter))
{
var groups = searcher.FindAll();
}
.Dump()
”是从哪里来的?我没有在任何地方看到它被定义。http://msdn.microsoft.com/library/system.directoryservices.accountmanagement.principalcontext.aspx - Lance U. MatthewsDump()
是我在LinqPad中测试时留下的。我已经从答案中删除了它。 - Andrew Cooper
System.DirectoryServices
命名空间。 - L.B