2014-02-21 18 views
0

我想找到一種方法來動態地返回用戶管理員的電子郵件地址。基本功能是一個下拉列表,用於在AD中選擇正確的OU併爲該OU構建一個數據表用戶。然後我有第二個下拉列表顯示該數據表中的用戶。 當選擇一個部門(OU)/用戶時,我會說這是用戶管理器的電子郵件地址到我的數據表中。任何建議表示讚賞,謝謝!asp.net LDAP問題,試圖返回用戶經理的電子郵件地址

我的下拉列表/查詢當前的代碼

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 
+0

我也應該注意到,ddbranch是項目的靜態列表,在每個項目我都在值LDAP連接字符串。這就是我選擇爲用戶查看的OU。 – Opiwan

回答

0

有包含經理的DN(辨別名)經理屬性。爲了得到他的電子郵件,您需要爲給定的DN創建一個新的DirectoryEntry並獲取郵件屬性。

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 
相關問題