Creativity and Innovation

Just another WordPress.com weblog


Leave a comment

Authenticate User and Password with Active Directory


Option Explicit

Sub Test()
Dim status As Boolean

status = authenticateWindowsLogin(“GANESH”, “pass@1273”, “home”)

If (status = True) Then
Debug.Print “User is authenticated successfully”
Else
Debug.Print “User is not authenticated”
End If

End Sub

‘Authenticates user and password entered with Active Directory.
Private Function authenticateWindowsLogin(ByVal strUserName As String, ByVal strpassword As String, ByVal strDomain As String) As Boolean

On Error GoTo ERROR_BLOCK

Dim oADsObject As Object, oADsNamespace As Object
Dim strADsPath As String

strADsPath = “WinNT://” & strDomain
Set oADsObject = GetObject(strADsPath)
Set oADsNamespace = GetObject(“WinNT:”)
Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, strDomain & “\” & strUserName, strpassword, 0)

authenticateWindowsLogin = True ‘ACCESS GRANTED

Exit_BLOCK:
On Error Resume Next
Exit Function

ERROR_BLOCK:
Debug.Print “Error Source:<” & Err.Source & “>,Function Name:”
Debug.Print “Error Number:<” & Err.Number & “>,Error Description:<” & Err.Description & “>”
authenticateWindowsLogin = False ‘ACCESS DENIED
Err.Clear
Resume Exit_BLOCK
End Function

Advertisements