Active directory 将语句与directoryservices一起使用
您能否帮助我,告诉我,我是否在我的directoryservice函数中正确使用了“using语句”,该函数可以从我的Active Directory中区分名称。我想正确处理和关闭对象 代码:Active directory 将语句与directoryservices一起使用,active-directory,directoryservices,using-statement,Active Directory,Directoryservices,Using Statement,您能否帮助我,告诉我,我是否在我的directoryservice函数中正确使用了“using语句”,该函数可以从我的Active Directory中区分名称。我想正确处理和关闭对象 代码: Public Function GetObjectDistinguishedName(ByVal objClass As objectClass, _ ByVal returnValue As returnType, _ ByVal objName As String, ByVal L
Public Function GetObjectDistinguishedName(ByVal objClass As objectClass, _
ByVal returnValue As returnType, _
ByVal objName As String, ByVal LdapDomain As String, _
Optional ByVal includeLdapPrefix As Boolean = True) As String
Dim distinguishedName As String = String.Empty
Dim connectionPrefix = "LDAP://" & LdapDomain
Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
Dim mySearcher = New DirectorySearcher(entry)
Dim result As SearchResult
Dim directoryObject As DirectoryEntry
Select Case objClass
Case objectClass.user
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + objName + ")(sAMAccountName=" + objName + ")))"
Case objectClass.group
mySearcher.Filter = "(&(objectClass=group)(|(cu=" + objName + ")(dn=" + objName + ")))"
Case objectClass.computer
mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + objName + ")(dn=" + objName + ")))"
Case objectClass.organizationalunit
mySearcher.Filter = "(ou=" + objName + ")"
End Select
result = mySearcher.FindOne()
If result Is Nothing Then 'If the search results in nothing, call for help!'
Throw New NullReferenceException("Unable to locate the distinguishedName for the " & objClass.ToString & " : " & objName & " in the " & LdapDomain & " domain")
End If
directoryObject = result.GetDirectoryEntry()
If returnValue.Equals(returnType.distinguishedName) Then
If includeLdapPrefix Then
distinguishedName = "LDAP://" & directoryObject.Properties("distinguishedName").Value
Else
distinguishedName = directoryObject.Properties("distinguishedName").Value
End If
End If
End Using
Return distinguishedName
End Function
一般来说,您应该始终对实现的类型调用
Dispose
。DirectoryEntry
和DirectorySearcher
都实现了IDisposable
。在代码示例中,只有第一个DirectoryEntry
对象被释放。您还需要为mysearch
和directoryObject
添加一个using块:
Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
Using mySearcher = New DirectorySearcher(entry)
'...'
Using directoryObject = result.GetDirectoryEntry()
'...'
End Using
End Using
End Using
实际上,您可以通过不使用GetDirectoryEntry
而直接从搜索结果中以folling方式检索“DiscriminatedName”(此代码未经测试,因为我当前不在域中):
一般来说,您应该始终对实现的类型调用
Dispose
。DirectoryEntry
和DirectorySearcher
都实现了IDisposable
。在代码示例中,只有第一个DirectoryEntry
对象被释放。您还需要为mysearch
和directoryObject
添加一个using块:
Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
Using mySearcher = New DirectorySearcher(entry)
'...'
Using directoryObject = result.GetDirectoryEntry()
'...'
End Using
End Using
End Using
实际上,您可以通过不使用GetDirectoryEntry
而直接从搜索结果中以folling方式检索“DiscriminatedName”(此代码未经测试,因为我当前不在域中):
我的搜索引擎怎么了?Using语句是否对此进行了处理?将代码添加到您的问题中,而不是作为明天可能会消失的外部链接。您有具体的理由问吗??就我所知,我觉得很好。。。您看到问题了吗?是的,当我在Active Directory中进行大量搜索时,会收到错误消息“服务器无法运行”。我认为这是因为有太多的连接同时运行。如果我正确地打开和关闭连接,也许我可以解决这个问题?mySearcher会发生什么情况?Using语句是否对此进行了处理?将代码添加到您的问题中,而不是作为明天可能会消失的外部链接。您有具体的理由问吗??就我所知,我觉得很好。。。您看到问题了吗?是的,当我在Active Directory中进行大量搜索时,会收到错误消息“服务器无法运行”。我认为这是因为有太多的连接同时运行。如果我正确地打开和关闭连接,也许我可以解决这个问题?非常感谢!我有一个类似的获取目录条目路径(AdsPath)的方法,不使用GetDirectoryEntry是否可以获取路径?@EasyDot:我不确定我是否理解你的问题。DifferentiedName不等于AdsPath吗?非常感谢!我有一个类似的获取目录条目路径(AdsPath)的方法,不使用GetDirectoryEntry是否可以获取路径?@EasyDot:我不确定我是否理解你的问题。DifferentiedName是否不等于AdsPath?