แบบสอบถามอย่างง่ายของ LDAP Vb.net

ฉันกำลังพยายามสร้างโค้ด vb.net สำหรับการสืบค้นแบบง่ายๆ ผ่าน LDAP แต่มีปัญหาและหาไม่พบว่ามันอยู่ที่ไหน

                Dim ldapServerName As String = "xxx.test.intranet.xxx.ca"
                Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/c=ca, DC=xxx,DC=corp,DC=xxx,DC=ca")
                oRoot.Username = "ou=Tool,ou=applications,o=xxx,c=ca"
                oRoot.Password = "something@2015"

                Dim LDAPSearcher As New DirectorySearcher()
                LDAPSearcher.Filter = "(&(employeenumber=6012589))"

                Dim SearchResult As SearchResult = LDAPSearcher.FindOne()
                Dim UserEntry As DirectoryEntry = SearchResult.GetDirectoryEntry()
                EDTEST.Text = UserEntry.Properties("employeenumber").Value.ToString

มันทำให้ฉันมีข้อผิดพลาดโดยบอกว่าวัตถุไม่ถูกต้อง ที่จริงแล้วตัวแปรผู้ค้นหาว่างเปล่า ดังนั้นจึงต้องเกี่ยวข้องกับการสืบค้นของฉันด้วย

นี่เป็นครั้งแรกที่ฉันใช้ LDAP¨ และฉันได้ลองวิธีแก้ปัญหาบางอย่างที่หาได้จากอินเทอร์เน็ตแล้ว แต่จนถึงขณะนี้ก็ยังไม่มีอะไรทำงาน

ข้อผิดพลาด: วัตถุไม่ได้ตั้งค่าเป็นอินสแตนซ์ของวัตถุ


person Johnny Prescott    schedule 04.01.2016    source แหล่งที่มา


คำตอบ (1)


เว้นแต่ว่าคุณกำลังเพิ่มแอตทริบิวต์อื่นเพื่อค้นหา คุณไม่จำเป็นต้องมีตัวดำเนินการ AND ในรูปแบบตัวกรองของคุณ การค้นหาเพียง (employeenumber=6012589) น่าจะใช้ได้ดี

หากคุณมีแอตทริบิวต์อื่นที่คุณต้องการใช้ค้นหา ไวยากรณ์ของตัวกรองจะคล้ายกับที่คุณมีตอนนี้ เฉพาะกับแอตทริบิวต์เพิ่มเติมเท่านั้น :

(&(employeenumber=6012589)(objectClass=user))

แก้ไข:

ฉันรวบรวมตัวอย่างโดยใช้เนมสเปซระดับล่าง System.DirectoryServices และ System.DirectoryServices.Protocols ซึ่งจะช่วยแยกฟังก์ชันการเข้าสู่ระบบและการค้นหาจริงออกไป และยังให้บริบทที่ดีขึ้นเมื่อเกิดข้อผิดพลาดอีกด้วย ตัวอย่างเช่น ฉันได้แทนที่ตัวแปรทั้งหมดด้วยตัวแปรที่คุณใช้ในคำถามของคุณ ฉันทดสอบสิ่งนี้กับอินสแตนซ์ Active Directory ของเราเองผ่านพอร์ต 389 ที่ไม่ปลอดภัยโดยใช้ข้อมูลประจำตัวของฉันและโดเมนพื้นฐานที่คล้ายกับโดเมนที่คุณใช้อยู่

Imports System.DirectoryServices.Protocols
Imports System.Net

Module Module1

Sub Main()

    ' setup your creds, domain, and ldap prop array 
    Dim username As String = "ou=Tool,ou=applications,o=xxx,c=ca"
    Dim pwd As String = "something@2015"
    Dim domain As String = "DC=xxx,DC=corp,DC=xxx,DC=ca"
    Dim propArray() As String = {"employeenumber"}

    ' setup your ldap connection, and domain component
    Dim ldapCon As LdapConnection = New LdapConnection("xxx.test.intranet.xxx.ca:389")
    Dim networkCreds As NetworkCredential = New NetworkCredential(username, pwd, domain)

    ' configure the connection and bind
    ldapCon.AuthType = AuthType.Negotiate
    ldapCon.Bind(networkCreds)

    ' if the above succceeded, you should now be able to issue search requests directly against the directory
    Dim searchRequest = New SearchRequest(domain, "(employeenumber=6012589)", SearchScope.Subtree, propArray)

    ' issue the search request, and check the results
    Dim searchResult As SearchResponse = ldapCon.SendRequest(searchRequest)
    Dim searchResultEntry As SearchResultEntry

    If (searchResult.Entries.Count > 0) Then ' we know we've located at least one match from the search

        ' if you're only expecting to get one entry back, get the first item off the entries list
        searchResultEntry = searchResult.Entries.Item(0)

        ' continue to do whatever processing you wish against the returned SearchResultEntry

    End If

End Sub

End Module
person X3074861X    schedule 04.01.2016
comment
ก็ยังไม่ทำงานอย่างที่ควรจะเป็น มันเหมือนกับว่ามันไม่สามารถหาสิ่งที่ควรจะเป็นได้ อย่างไรก็ตาม หากฉันใช้ LDAP Admin และเชื่อมต่อสิ่งนี้ ฉันจะพบได้อย่างชัดเจนว่ามีรายการอยู่ที่นั่น - person Johnny Prescott; 05.01.2016
comment
อืม... คุณรู้ไหมว่ามันอาจจะไม่ใช่ความคิดที่ดีที่จะแบ่งตรรกะของคุณสักหน่อย ฉันสามารถใส่ตัวอย่าง VB.Net ว่าฉันทำเช่นนี้ใน C # ได้อย่างไร - คำถามด่วน คุณกำลังพยายามเชื่อมต่ออย่างปลอดภัยผ่าน LDAPS (พอร์ต 636) หรือ LDAP ที่ไม่ปลอดภัยผ่านพอร์ต 389 หรือไม่ - person X3074861X; 05.01.2016