将语句与目录服务一起使用

发布于 2024-09-13 00:16:01 字数 2080 浏览 0 评论 0原文

您能否帮助我并告诉我我是否在从 Active Directory 获取专有名称的目录服务功能中正确使用了“using 语句”。我想正确处置和关闭物体。

代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-09-20 00:16:01

作为一般规则,您应该始终对实现 IDisposableDirectoryEntryDirectorySearcher 都实现了 IDisposable。在您的代码示例中,只有第一个 DirectoryEntry 对象被释放。您还需要为 mySearcherdirectoryObject 添加一个 using 块:

Using entry As DirectoryEntry = New DirectoryEntry(connectionPrefix)
    Using mySearcher = New DirectorySearcher(entry)
        '...'
        Using directoryObject = result.GetDirectoryEntry()
            '...'
        End Using
    End Using
End Using

您实际上可以通过不使用 GetDirectoryEntry 减轻服务器上的负载而是通过以下方式直接从搜索结果中检索“distinguishedName”(此代码未经测试,因为我当前不在域上):

mySearcher.PropertiesToLoad.Add("distinguishedName");
result = mySearcher.FindOne()
'...'
distinguishedName = result.Properties("distinguishedName")(0)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文