我试图通过LDAP为一个简单的查询创建一个vb.net代码,但遇到了问题,找不到它在哪里。
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
它给了我一个错误,说对象无效。searcher变量实际上是空的,所以它在某种程度上与我的查询有关。
这是我第一次使用LDAP,我已经尝试了一些在网上可以找到的解决方案,但到目前为止都不起作用。
错误:对象未设置为对象的实例。
除非您要添加另一个属性进行搜索,否则您的过滤器语法中不需要AND运算符——简单搜索(employeenumber=6012589)
应该可以正常工作。
如果你有另一个想要搜索的属性,过滤器语法将与你现在的类似,只是有一个额外的属性:
(&(employeenumber=6012589)(objectClass=user))
编辑:
我使用较低级别的System.DirectoryServices
和System.DirectoryServices.Protocols
名称空间组合了一个示例。这有助于分解实际的登录和搜索功能,并在出现错误时提供更好的上下文。例如,我已经用你在问题中使用的变量替换了我的所有变量。我使用我的信用证和与您使用的基本域类似的基本域,在不安全的端口389上针对我们自己的Active Directory实例进行了测试。
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