问题描述
您能帮我一下吗,请问我是否在我的目录服务功能中正确使用了使用语句,该功能从Active Directory中获得了可分辨的名称。我要正确放置和关闭对象。
Could you help me and tell if im using the "using statement" correctly in my directoryservice function that gets distingushed name from my Active Directory. I want to dispose and close objects correctly.
代码:
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 .aspx rel = nofollow> IDisposable
。 DirectoryEntry
和 DirectorySearcher
均实现 IDisposable
。在您的代码示例中,仅第一个 DirectoryEntry
对象被处置。您还需要为 mySearcher
和 directoryObject
添加using块:
As a general rule you should always call Dispose
on types that implement IDisposable
. Both DirectoryEntry
and DirectorySearcher
implement IDisposable
. In your code example only the first DirectoryEntry
object gets disposed. You need to add a using block for mySearcher
and directoryObject
as well:
Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
Using mySearcher = New DirectorySearcher(entry)
'...'
Using directoryObject = result.GetDirectoryEntry()
'...'
End Using
End Using
End Using
您实际上可以通过不使用 GetDirectoryEntry
来减轻服务器的负载,而直接从服务器中检索 distinguishedName以以下方式搜索结果(此代码未经测试,因为我当前不在域中):
You may actually lighten the load on your server a bit by not using GetDirectoryEntry
and instead retrieve "distinguishedName" directly from the search result in the folling way (this code is untested as I am not currently on a domain):
mySearcher.PropertiesToLoad.Add("distinguishedName");
result = mySearcher.FindOne()
'...'
distinguishedName = result.Properties("distinguishedName")(0)
这篇关于在目录服务中使用语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!