Resources » Code Snippets » Active Directory

Authenticate user against active directory

Updated: Category: Active Directory
Author: Member Level: GoldPoints: 10

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

Did you like this resource? Share it with your friends and show your love!

Responses to "Authenticate user against active directory"
Author: Manindra Kumar Upadhyay  05 Apr 2010   Member Level: Gold   Points : 1
try this:


Post Comment:

  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India