asp.net LDAP问题,试图返回用户经理的电子邮件地址

时间:2014-02-21 16:53:59

标签: asp.net vb.net ldap ldap-query

我正在尝试找到一种动态返回用户管理器电子邮件地址的方法。基本功能是一个下拉列表,用于在AD中选择正确的OU并为该OU构建用户的数据表。然后我有第二个下拉列表显示该数据表中的用户。 当选择部门(OU)/用户时,我想将该用户经理的电子邮件地址提取到我的数据表中。任何建议都表示赞赏,谢谢!

我目前的下拉列表/查询代码是

    Protected Sub ddbranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddbranch.SelectedIndexChanged

     Dim searchRoot As New DirectoryEntry(ddbranch.SelectedValue)
         Using searchRoot
            Dim searchFilter As String = "((&(&(&(objectclass=user)(objectcategory=person))(!(userAccountControl=514)(!(!Description=*))))))"
            searchRoot.Username = "username"
            searchRoot.Password = "password"
            Dim ds As New DirectorySearcher(searchRoot, searchFilter)
            ds.PageSize = 500
            Using searchResults As SearchResultCollection = ds.FindAll()
                If searchResults.Count > 0 Then
                    ' make the users table and its columns
                    Dim tblusers As New DataTable("users")

                    ' Holds the columns and rows 
                    Dim col As DataColumn
                    Dim row As DataRow


                    ' Create FullName column
                    col = New DataColumn("FullName", System.Type.[GetType]("System.String"))
                    col.DefaultValue = ""
                    tblusers.Columns.Add(col)
                    'Create Manager column
                    col = New DataColumn("mnger", System.Type.[GetType]("System.String"))
                    col.DefaultValue = ""
                    tblusers.Columns.Add(col)
                    ' Create Email column
                    col = New DataColumn("Email", System.Type.[GetType]("System.String"))
                    col.DefaultValue = ""
                    tblusers.Columns.Add(col)


                    ' Iterate over all the results in the resultset.
                    For Each result As SearchResult In searchResults
                        row = tblusers.NewRow()

                        ' Display Name
                        If result.Properties.Contains("displayName") Then
                            row("FullName") = result.Properties("displayName")(0).ToString()
                        End If

                        If result.Properties.Contains("mail") Then
                            If result.Properties("mail")(0).ToString() <> "" Then
                                Dim email As String = result.Properties("mail")(0).ToString()
                                row("Email") = "<a href=""mailto:" + email + """ title=""Send mail to " + email + """>" + email + "</a>"
                            End If
                        End If


                        '   If there is actually something to display, create a new table row.
                        If row("FullName") <> "" And row("Email") <> "" Then
                            tblusers.Rows.Add(row)
                        End If
                    Next
                    ' instantiate a new DataSet object that
                    Dim dataSet As New DataSet()
                    dataSet.Tables.Add(tblusers)
                    ddempname.Visible = True


                    With ddempname
                        .Items.Clear()
                        .Items.Add(New ListItem("Please Select Name", "-1"))
                        ' .Items.Add(New ListItem("ATM", "ATM"))
                        ' .Items.Add(New ListItem("Vault", "Vault"))
                        ' .Items.Add(New ListItem("Temp Teller", "Temp Teller"))
                        .AppendDataBoundItems = True



                        ddempname.DataSource = dataSet.Tables("users")
                        .DataTextField = "FullName"
                        .DataValueField = "FullName"
                        ddempname.DataBind()
                    End With

                End If

            End Using
        End Using
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

有一个manager-property包含管理器的DN(可分辨名称)。要获取他的电子邮件,您需要为给定的DN创建一个新的DirectoryEntry并获取mail-property。

实施例

If result.Properties.Contains("manager") Then
     Dim manager As New DirectoryEntry(result.Properties("manager")(0).ToString())
     row("mnger") = manager.Properties("mail")(0).ToString()
End If