|
-
Feb 8th, 2010, 11:24 AM
#23
Re: Validate Login against Active Directory
 Originally Posted by jinx101
Just a note on the original code. If you use that code in any kind of loop the AD objects need to be disposed of because you'll leak lots of memory otherwise (speaking from personal experience):
Code:
Public Shared Function ValidateLogin(ByVal domain As String, ByVal username As String, ByVal password As String) As Boolean
Dim success As Boolean = False
Dim entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & domain, username, password)
Dim searcher As New System.DirectoryServices.DirectorySearcher(entry)
searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
Try
Dim results As System.DirectoryServices.SearchResult = searcher.FindOne
success = Not (results Is Nothing)
Catch ex As Exception
success = False
Finally
entry.Close() : entry.Dispose()
searcher.Dispose()
End Try
Return success
End Function
That is a good point but one minor comment - you don't need to call Close and Dispose on the DirectoryEntry object because they both do pretty much exactly the same thing. The Close method just calls an internal method named Unbind (which frees up the resources that were used) and the Dispose method calls the same Unbind method but then also calls MyBase.Dispose. So I would just go with Dispose on its own and not bother with Close
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|