Using statement with directoryservices

南笙酒味 提交于 2019-12-11 14:53:47

问题


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.

Code:

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

回答1:


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

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)


来源:https://stackoverflow.com/questions/3421934/using-statement-with-directoryservices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!