Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET

Simple Active Directory Authentication Using LDAP and ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.05/5 (14 votes)
16 Mar 2010CPOL1 min read 261.4K   57   17
Quick and easy Active Directory authentication using LDAP and ASP.NET

Introduction

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).

Background

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:

VB.NET
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://mydomain.com.
  • user: The user's account name. Can be prefixed by the domain; e.g., mydomain\tom or just tom.
  • pass: The user's password.
VB.NET
Function AuthenticateUser(path as String, user As String, pass As String) As Boolean
    Dim de As New DirectoryEntry(path, user, pass, AuthenticationTypes.Secure)
    Try 
        'run a search using those credentials.  
        'If it returns anything, then you're authenticated
        Dim ds As DirectorySearcher = New DirectorySearcher(de)
        ds.FindOne()
        Return True
    Catch
        '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.

License

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


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthanks Pin
Jay Agno25-Apr-23 21:14
Jay Agno25-Apr-23 21:14 
QuestionUse of LDAP queries in "Windows" authentication mode in asp.net Pin
Rasik Bihari Tiwari12-Jan-16 20:31
professionalRasik Bihari Tiwari12-Jan-16 20:31 
QuestionI have been trying to get this up and running, but it always fails with false. Pin
grantrat16-Nov-15 17:06
grantrat16-Nov-15 17:06 
QuestionSaved my life Thanks. I was trying another method Pin
marc Karam10-Apr-15 0:20
marc Karam10-Apr-15 0:20 
QuestionSmart thanks , but need Group Check Pin
mahdi faiq15-Mar-14 0:00
mahdi faiq15-Mar-14 0:00 
GeneralMy vote of 5 Pin
Eddy Jawed4-Sep-13 5:53
Eddy Jawed4-Sep-13 5:53 
small, simple and sweet
QuestionThis code is superb, simple and all i needed! thank you! Pin
Eddy Jawed4-Sep-13 5:52
Eddy Jawed4-Sep-13 5:52 
GeneralSuccess Pin
Member 814812916-Oct-12 4:22
Member 814812916-Oct-12 4:22 
GeneralMy vote of 5 Pin
keibo842-May-12 2:31
keibo842-May-12 2:31 
QuestionAbout accoun rights Pin
c_sharp_developer4-Sep-07 21:59
c_sharp_developer4-Sep-07 21:59 
QuestionAuthentication Pin
duentm23-Jul-07 14:00
duentm23-Jul-07 14:00 
GeneralCharacter errors & Pin
largeinnit9-Jul-07 5:14
largeinnit9-Jul-07 5:14 
GeneralSecurity Pin
Craster29-Jun-07 1:33
Craster29-Jun-07 1:33 
GeneralRe: Security Pin
Jesse Fatherree16-Mar-10 7:23
Jesse Fatherree16-Mar-10 7:23 
GeneralWorks fine for simple passwords, but still some questions Pin
largeinnit29-Jun-07 0:14
largeinnit29-Jun-07 0:14 
GeneralThank you... Pin
KnotBeer1-Jun-07 9:26
KnotBeer1-Jun-07 9: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.