<%
'==========================
' AD_Auth_Class.asp
'==========================
Class AD_Auth_Class
  Public DomainName      ' e.g. yourdomain.local
  Public ContainerDN     ' e.g. DC=yourdomain,DC=local
  Public Username
  Public Password
  Public ErrorMessage
  Public IsAuthenticated
  Public UserObject

  Private Sub Class_Initialize()
    IsAuthenticated = False
    ErrorMessage = ""
    Set UserObject = Nothing
  End Sub

  Public Function Authenticate()
    On Error Resume Next

    Dim ldapPath, userCredential
    ldapPath = "LDAP://" & ContainerDN  ' Must be DC=yourdomain,DC=local
    userCredential = DomainName & "\" & Username

    ' Try to bind to Active Directory
    Dim rootDSE : Set rootDSE = GetObject("LDAP:")
    Set UserObject = rootDSE.OpenDSObject(ldapPath, userCredential, Password, 1)

    If Err.Number <> 0 Then
      ErrorMessage = "Authentication failed: " & Err.Description
      IsAuthenticated = False
      Set UserObject = Nothing
    Else
      IsAuthenticated = True
      ErrorMessage = ""
    End If

    Authenticate = IsAuthenticated
    On Error GoTo 0
  End Function
End Class

dim AD_Auth_Class__Singleton
Function AdAuth()
    if IsEmpty(AD_Auth_Class__Singleton) then
        set AD_Auth_Class__Singleton = new AD_Auth_Class
    end if
    set AdAuth = AD_Auth_Class__Singleton
End Function
%>
