【问题标题】:Querying LDAP in VB.NET. I have the user account, and I want a list of groups the user in in在 VB.NET 中查询 LDAP。我有用户帐户,我想要用户所在的组列表
【发布时间】:2013-07-08 15:48:14
【问题描述】:

我知道 SAMAccountName,现在想用反映此用户在整个目录中的组成员身份的条目来填充组列表。这是我的开始,但我很难过:

        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

我不知道如何遍历它。求指点?

【问题讨论】:

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


    【解决方案1】:

    如果您使用的是 .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 化,但是是的,这是一个非常棒的指针,比你好多了。
    【解决方案2】:

    memberOf 属性具有distinguished name 语法,并且是该用户所属的组的 DN。换句话说,如果条目具有memberOf 属性,并且该属性的值是有效的组 DN,则用户已经是该组的成员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2011-07-12
      • 2020-10-20
      相关资源
      最近更新 更多