从LDAP获取AD ID信息

时间:2012-10-11 20:36:53

标签: vb.net active-directory ldap windows-server-2003

我将以下代码用于Web ASP.net应用程序,并在本地工作正常,但在服务器上却没有。它返回一个错误,表示“表不存在”。在我的网络配置中,我放了<identity impersonate="true"/> 有什么想法吗?

#Region " Get AD ID information from LDAP"
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim objRootDSE, objRecordSet, objCommand, objConnection, strDNSDomain As Object
    Dim strQuery, strBase, strFilter, strAttributes As Object
    Dim intCountNumber As Integer

    Try
        ' Determine DNS domain name from RootDSE object.
        objRootDSE = GetObject("LDAP://RootDSE")
        strDNSDomain = objRootDSE.Get("defaultNamingContext")

        ' Use ADO to search Active Directory.
        objRecordSet = Server.CreateObject("ADODB.recordset")
        objCommand = Server.CreateObject("ADODB.Command")
        objConnection = Server.CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open("Active Directory Provider")
        objCommand.ActiveConnection = objConnection

        strBase = "<LDAP://" & strDNSDomain & ">"
        strFilter = "(&(objectCategory=person)(objectClass=user)(samaccountname=" & TB_UserID.Text & "))"
        strAttributes = "sn,givenname,mail,telephoneNumber,"
        strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
        objCommand.CommandText = strQuery
        objRecordSet = objCommand.Execute
        intCountNumber = objRecordSet.Fields.Count
        TB_Name.Text = objRecordSet.Fields("givenname").value
        TB_LastName.Text = objRecordSet.Fields.Item("sn").value
        TB_Email.Text = objRecordSet.Fields.Item("mail").value
        TB_Telephone.Text = objRecordSet.Fields.Item("telephoneNumber").value

        objRecordSet = Nothing
        objConnection.Close()
    Catch ex As Exception
        DisplayWebMsgBox.WebMsgBox.MessageBox(ex.Message)
    End Try

End Sub

结束地区

1 个答案:

答案 0 :(得分:0)

我发现使用提供程序“ADsDSOObject”,它使用用户creditentials来运行LDAP搜索。因此,除非我提供用户名和密码信用额度,否则从服务器端无法工作。

    objConnection.Properties("User ID") = Username
objConnection.Properties("Password") = Password
 objConnection.Properties("Encrypt Password") = True