Authenticate user against active directory

Public Function IsAuthenticated(ByVal username As String, ByVal pwd As String) As Boolean

Dim entry As DirectoryEntry = New DirectoryEntry(_path, username, pwd)
entry.AuthenticationType = AuthenticationTypes.Secure
entry.Path = _path

'Bind to the native AdsObject to force authentication.

Dim obj As Object = entry.NativeObject
Dim search As DirectorySearcher = New DirectorySearcher(entry)

search.Filter = "(sAMAccountName=" & username & ")"


Dim result As SearchResult = search.FindOne()
Dim info As DirectoryEntry = result.GetDirectoryEntry
' Label4.Text=CType(info.Properties("displayName").?? String.Empty, String)

If (result Is Nothing) Then
Return False

End If

'Update the new path to the user in the directory.
_path = result.Path
_filterAttribute = CType(result.Properties("cn")(0), String)
'_filterAttribute = CType(result.Properties("givenName")(1), String)

Catch ex As Exception
Throw New Exception("Error authenticating user. " & ex.Message)
End Try
' WriteLine(_filterAttribute)

Return True

End Function


Author: Manindra Kumar Upadhyay05 Apr 2010 Member Level: Gold   Points : 1

try this:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: