Click here to Skip to main content
11,927,378 members (52,129 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked

Simple Active Directory Authentication Using LDAP and ASP.NET

, 16 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Quick and easy Active Directory authentication using LDAP and ASP.NET


I searched the web high and low for Active Directory authentication using VB.NET, and all of the articles I found used the impersonate model to do LDAP queries. However, using the System.DirectoryServices.dll library, there's a simple function that does all of the work for you. This function basically takes a username and password, and tries to authenticate it on the given LDAP (Active Directory).


Instead of writing 20-30 lines of inefficient code, I wanted to make something very short and simple. All this function does is attempt to create an LDAP object using the given credentials. If it fails, then the username/password combination is invalid.

The Code

First off, you need to make sure to reference the System.DirectoryServices.dll. Then, include the library in your page, using:

Imports System.DirectoryServices

You can use this function along with forms based authentication or just to check a user's credentials. It takes the following input variables:

  • path: An LDAP path for the FQDN of your AD. E.g., LDAP://
  • user: The user's account name. Can be prefixed by the domain; e.g., mydomain\tom or just tom.
  • pass: The user's password.
Function AuthenticateUser(path as String, user As String, pass As String) As Boolean
    Dim de As New DirectoryEntry(path, user, pass, AuthenticationTypes.Secure)
        'run a search using those credentials.  
        'If it returns anything, then you're authenticated
        Dim ds As DirectorySearcher = New DirectorySearcher(de)
        Return True
        'otherwise, it will crash out so return false
        Return False
    End Try 
End Function

The function returns a simple True/False if it successfully binds to the LDAP using the given credentials.

***Update*** I added the AuthenticationType.Secure to enable the Kerberos/NTLM encryption of the data as it's passed along the network. I also changed the function to actually search for an object instead of just using the NativeObject binding. With the updated code, I've verified that no clear text is passed (using Network Monitor) and it also works with passwords using symbols.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jesse Fatherree
Systems Engineer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionI have been trying to get this up and running, but it always fails with false. Pin
grantrat16-Nov-15 18:06
membergrantrat16-Nov-15 18:06 
QuestionSaved my life Thanks. I was trying another method Pin
marc Karam10-Apr-15 1:20
membermarc Karam10-Apr-15 1:20 
QuestionSmart thanks , but need Group Check Pin
mahdi faiq15-Mar-14 1:00
membermahdi faiq15-Mar-14 1:00 
GeneralMy vote of 5 Pin
Eddy Jawed4-Sep-13 6:53
memberEddy Jawed4-Sep-13 6:53 
QuestionThis code is superb, simple and all i needed! thank you! Pin
Eddy Jawed4-Sep-13 6:52
memberEddy Jawed4-Sep-13 6:52 
GeneralSuccess Pin
Member 814812916-Oct-12 5:22
memberMember 814812916-Oct-12 5:22 
GeneralMy vote of 5 Pin
keibo842-May-12 3:31
memberkeibo842-May-12 3:31 
QuestionAbout accoun rights Pin
c_sharp_developer4-Sep-07 22:59
memberc_sharp_developer4-Sep-07 22:59 
QuestionAuthentication Pin
duentm23-Jul-07 15:00
memberduentm23-Jul-07 15:00 
GeneralCharacter errors & Pin
largeinnit9-Jul-07 6:14
memberlargeinnit9-Jul-07 6:14 
GeneralSecurity Pin
Craster29-Jun-07 2:33
memberCraster29-Jun-07 2:33 
GeneralRe: Security Pin
Jesse Fatherree16-Mar-10 8:23
memberJesse Fatherree16-Mar-10 8:23 
GeneralWorks fine for simple passwords, but still some questions Pin
largeinnit29-Jun-07 1:14
memberlargeinnit29-Jun-07 1:14 
GeneralThank you... Pin
KnotBeer1-Jun-07 10:26
memberKnotBeer1-Jun-07 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 16 Mar 2010
Article Copyright 2007 by Jesse Fatherree
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid