Private Function UserExistsInAD(ByVal domainName As String, ByVal userName As String) As Boolean
If domainName Is Nothing Then
Throw New ArgumentNullException("domainName", "Domain Name can not be null")
End If
If userName Is Nothing Then
Throw New ArgumentNullException("userName", "User Name can not be null.")
End If
Try
Dim search As New DirectorySearcher(New DirectoryEntry([String].Format("LDAP://{0}", domainName), Nothing, Nothing))
search.Filter = [String].Format("(|(cn={0})(SAMAccountName={0}))", userName)
search.PropertiesToLoad.Add("cn")
search.PropertiesToLoad.Add("samaccountname")
search.PropertiesToLoad.Add("givenname")
search.PropertiesToLoad.Add("sn")
search.PropertiesToLoad.Add("memberOf")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
Else
Return True
End If
Catch e As Exception
Throw e
End Try
End Function
|