LDAP Vb.net 简单查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34600846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:39:38  来源:igfitidea点击:

LDAP Vb.net simple query

vb.netldap

提问by Johnny Prescott

I'm trying to create a vb.net code for a simple query through LDAP but having an issue and can't find where it is.

我正在尝试通过 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

it is giving me an error saying that the object is not valid. The searcher variable is in fact empty so it has to do with my query somehow.

它给了我一个错误,说对象无效。searcher 变量实际上是空的,所以它以某种方式与我的查询有关。

This is my first time with LDAP¨and I have tried some of the solution i could find on the net but nothing is working so far.

这是我第一次使用 LDAP——我尝试了一些我可以在网上找到的解决方案,但到目前为止没有任何效果。

Error: Object not set to an instance of an object.

错误:对象未设置为对象的实例。

回答by X3074861X

Unless you're adding another attribute to search by, you don't need the ANDoperator in your filter syntax - a search for simply (employeenumber=6012589)should work just fine.

除非您添加另一个要搜索的属性,否则您 的过滤器语法中不需要AND运算符 - 搜索(employeenumber=6012589)应该可以正常工作。

If do you have another attribute you'd like to search by, the filter syntax would be similiar to what you have now, only with the additional attribute :

如果您有其他想要搜索的属性,过滤器语法将与您现在拥有的类似,只有附加属性:

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

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

EDIT:

编辑:

I put together an example using the lower level System.DirectoryServicesand System.DirectoryServices.Protocolsnamespaces. This helps break up the actual login and search functions, and will also provide better context when errors occur. For the example, I've replaced all of my variables with the ones you're using in your question. I tested this against our own Active Directory instance over unsecured port 389 using my creds and a base domain similar to the one you're using.

我将使用较低级别System.DirectoryServicesSystem.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