将语句与目录服务一起使用
您能否帮助我并告诉我我是否在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一般规则,您应该始终对实现
IDisposable
。DirectoryEntry
和DirectorySearcher
都实现了IDisposable
。在您的代码示例中,只有第一个DirectoryEntry
对象被释放。您还需要为mySearcher
和directoryObject
添加一个 using 块:您实际上可以通过不使用
GetDirectoryEntry
减轻服务器上的负载而是通过以下方式直接从搜索结果中检索“distinguishedName”(此代码未经测试,因为我当前不在域上):As a general rule you should always call
Dispose
on types that implementIDisposable
. BothDirectoryEntry
andDirectorySearcher
implementIDisposable
. In your code example only the firstDirectoryEntry
object gets disposed. You need to add a using block formySearcher
anddirectoryObject
as well: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):