Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-06-24
1
Medium Priority
?
623 Views
Last Modified: 2013-12-24
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
Const ADS_SECURE_AUTHENTICATION = 1
Const ADS_SERVER_BIND = 512

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"
oADOConn.Open

If SAMName = "" Then SAMName = "sAMAccountName"

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

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

oADORs.Close
Set oADORs = Nothing
oADOConn.Close
Set oADOConn = Nothing

If Not blnUserExists Then
    AuthenticateUser = False
    Exit Function
Else

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

AuthenticateUser = False
End If



End Function
0
Comment
Question by:mcbain942
1 Comment
 
LVL 1

Accepted Solution

by:
mre224 earned 2000 total points
ID: 21860342

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
 Else
 AuthenticateUser3 = 1
 End If

Exit Function

broke:
AuthenticateUser3 = -2
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question