vb.net - 在 VB.NET 中查询 LDAP。我有用户帐户,我想要一个用户所在组的列表

标签 vb.net active-directory ldap active-directory-group directorysearcher

我知道 SAMAccountName,现在想用反射(reflect)该用户在整个目录中的组成员资格的条目填充组列表。这是我的开始,但我很困惑:

        Dim path As String = WebConfigurationManager.AppSettings("ldapPath")
        Dim entry As New DirectoryEntry(path)
        Dim search As DirectorySearcher = New DirectorySearcher(entry)
        Dim groupList As StringBuilder = New StringBuilder()
        search.Filter = "(SAMAccountName=" & _thisUser.UserName & ")"
        search.PropertiesToLoad.Add("memberOf")
        'search.SearchScope = SearchScope.Subtree

        For Each res As SearchResult In search.FindAll
        Next  ''Just doing this so I can look at "res" objects in debug

我不知道如何遍历它。请问有什么指点吗?

最佳答案

如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, yourSamAccountName);

   if(user != null)
   {
        var groups = user.GetGroups();

        // iterate over groups or do whatever else you need to do....
   }
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩真的很容易!

关于vb.net - 在 VB.NET 中查询 LDAP。我有用户帐户,我想要一个用户所在组的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17530833/

相关文章:

c# - 来自 C# 的 Active Directory 查找在服务器上失败但在本地工作

linux - 未读取 openldap ACL

python - django ldap auth 无法从应用程序运行

vb.net - 保存和加载数据 Visual Basic 的简单方法

powershell - 一次处理4个循环?

windows - 让 AD 用户在 AD 的描述字段中具有特定关键字

rabbitmq - 如何使用 LDAP 为 AD 组配置 RabbitMQ?

.net - 给出完整文件路径的代码审查 : Determining whether a folder exists,?

mysql - 检查与您的 mariadb 服务器版本对应的手册,了解在第 1 行 "user"附近使用的正确语法

vb.net - 从类型 'String' 到类型 'String' 的转换无效