• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

How to Authenticate Username and/or password to any Microsoft or any Direcotry server with service account and with SSL

Im a little confused how to use use IADs to verify a username and password IF a company locks down thier Directory server with a service account username and password first before you can even connect.

The following code i have coded to work with encryption if the function is asking for it.  I would like this to work for any Directory server not just microsoft.  here is my code.  In your response if you could just give me pseudo code on how to do this with my object names i would appreciate it

This function already works on my NT AD as is, but it do not understand how to impliment sServiceAccount username and sSA password.

Thank you.

Function AuthenticateUser(strServerName As String, strUserName As String, blnSSL As Boolean, Optional strPassword As String, Optional sPort As String, Optional SAMName As String, Optional sServiceAccount As String, Optional sSAPassword) As Boolean
On Error Resume Next

Dim strUserADSPath As String
Dim blnUserExists As Boolean

Dim oADOConn As New ADODB.Connection
Dim oADORs As New ADODB.Recordset

Dim oUser As IADs
Dim oDSObj As IADsOpenDSObject
Dim strNamingContext As String

strServerName = strServerName & ":389/"
If sPort <> "" Then
strServerName = Replace(strServerName, ":389", ":" & sPort)
End If

Dim oRootDSE As IADs
    Set oRootDSE = GetObject("LDAP://" & strServerName & "RootDSE")
    strNamingContext = strServerName & oRootDSE.Get("defaultNamingContext")

Set oRootDSE = Nothing

strUserADSPath = ""
blnUserExists = False
Set oADOConn = CreateObject("ADODB.CONNECTION")
Set oADORs = CreateObject("ADODB.Recordset")
oADOConn.Provider = "ADSDSOObject"

If SAMName = "" Then SAMName = "sAMAccountName"

Set oADORs = oADOConn.Execute("<LDAP://" & strNamingContext & ">;(" & SAMName & "=" & strUserName & ");AdsPath, cn")
If oADORs.RecordCount = 0 Then

    strUserADSPath = oADORs.Fields("ADSPATH").value
    blnUserExists = True
End If

Set oADORs = Nothing
Set oADOConn = Nothing

If Not blnUserExists Then
    AuthenticateUser = False
    Exit Function

    If strPassword = "" Then
    AuthenticateUser = True
    Exit Function
    End If

End If

Dim oAuth

Set oUser = GetObject(strUserADSPath)
Set oDSObj = GetObject("LDAP:")

If blnSSL = True Then
Set oAuth = oDSObj.OpenDSObject("LDAP://" & strNamingContext, strUserName, strPassword, ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND)
Set oAuth = oDSObj.OpenDSObject("LDAP://" & strNamingContext, strUserName, strPassword, ADS_SERVER_BIND)
End If

If Err.Number <> 0 Then
AuthenticateUser = False
Exit Function
End If

If Not oAuth Is Nothing Then
    Set oAuth = Nothing
    AuthenticateUser = True

AuthenticateUser = False
End If

End Function
1 Solution

Function AuthenticateUser3(strServerName As String, dcContextFullString As String, sSamUNContextFieldName As String, sServiceAccountContextFieldName As String, sOranizationUnitFullString As String, strUserToAuth As String, strPwToAuth As String, Optional sServiceAccountUN As String, Optional sServiceAccountPW As String, Optional strPort As String) As Integer

On Error GoTo broke

Dim con As New ADODB.Connection
Dim rs
Dim com As New ADODB.command
Dim path As String
Dim user As String
Dim sstr As String
Set con = CreateObject("ADODB.Connection")

strServerName = strServerName & ":389/"
If strPort <> "" Then
strServerName = Replace(strServerName, "389", strPort)
End If

sstr = "<LDAP://" & strServerName & dcContextFullString & ">;(" & sSamUNContextFieldName & "=" & sServiceAccountUN & ");AdsPath," & sServiceAccountContextFieldName & ";subtree"

con.Provider = "ADSDSOObject"
con.Properties("User ID") = sServiceAccountContextFieldName & "=" & sServiceAccountUN & "," & sOranizationUnitFullString & "," & dcContextFullString
con.Properties("Password") = sServiceAccountPW
con.Properties("ADSI Flag") = 34
con.Open "ADSI"

Set com = CreateObject("ADODB.Command")
Set com.ActiveConnection = con
com.CommandText = sstr
Set rs = com.Execute

If rs.EOF Then
AuthenticateUser3 = -1
End If

Dim dso
Dim cont

 Set dso = GetObject("LDAP:")
 Set cont = dso.OpenDSObject("LDAP://" & strServerName & dcContextFullString, strUserToAuth, strPwToAuth, 513)

 If Err.Number <> 0 Then
 'MsgBox Err.Description
 AuthenticateUser3 = 0
 AuthenticateUser3 = 1
 End If

Exit Function

AuthenticateUser3 = -2
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now