User authentication over LDAP in asp - authentication

I want to pass username and password to LDAP and retrieve user information. I have code below but don't know where must I add password string in it?
strUsername = Request.Form("username")
strPassword = Request.Form("password")
Set rootDSE = GetObject("LDAP://RootDSE")
Set oConn = CreateObject("ADODB.Connection")
sDomainContainer = rootDSE.Get("defaultNamingContext")
Debug "DomainContainer: " & sDomainContainer
oConn.Properties("Encrypt Password") = true
oConn.Provider = "ADSDSOObject"
oConn.properties("user id") = sLdapReaderUsername
oConn.properties("password") = sLdapReaderPassword
oConn.Open "ADs Provider"
sQuery = "<LDAP://" & sDomainContainer & ">;(sAMAccountName=" & strUsername & ");adspath,mail,displayName;subtree"
Set userRS = oConn.Execute(sQuery)
If Not userRS.EOF and not err then
sFullName = userRS("displayName")
sEmail = userRS("mail")
sExternalID = ""
sOrganization = ""
Response.Write("sFullName: "&sFullName)
Response.Write("sEmail: "&sEmail)
.
..
...

This worked for me:
function AuthenticateUser(Username,Password,Domain)
dim strUser,strPass,strQuery,oConn,cmd,oRS
AuthenticateUser = false
strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE objectClass='*'"
set oConn = server.CreateObject("ADODB.Connection")
oConn.Provider = "ADsDSOOBJECT"
oConn.properties("User ID") = Username
oConn.properties("Password")=Password
oConn.properties("Encrypt Password") = true
oConn.open "DS Query", Username,Password
set cmd = server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = oConn
cmd.CommandText = strQuery
on error resume next
set oRS = cmd.Execute
if oRS.bof or oRS.eof then
AuthenticateUser = false
else
AuthenticateUser = true
end if
set oRS = nothing
set oConn = nothing
end function

Related

Data Mismatch error while using the Update query

I am using the Update query inside VBA to Update one particular column.
But I am getting the Data Mismatch in criteria expression error. Please find below the code.
Public Function UpdateDistinctColumnFRNumberBasis()
MergedInvoiceFile = "\test.xlsx"
StrInvoiceNumber = "010541-01"
FRSparepartNumber = "FT99999000006"
Application.ScreenUpdating = False
Application.Calculation = xlCalculateManual
Application.EnableEvents = False
Dim objConn As Object
Dim objRecordSet As Object
Set objConn = CreateObject("ADODB.Connection")
Set objRecCmd = CreateObject("ADODB.Command")
Set objRecCmd_Update = CreateObject("ADODB.Command")
objConn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MergedInvoiceFile & ";Extended Properties=""Excel 8.0;""")
strSQL = "Update [Tabelle1$] SET [Distinct] = 'Distinct' Where ([RECHNR] ='" & StrInvoiceNumber & "' AND [TEILENUMMER] = '" & FRSparepartNumber & "')"
objConn.Execute strSQL
objConn.Close
Set objConn = Nothing
Set objRecCmd = Nothing
Set objRecCmd_Update = Nothing
End Function

How to solve the system error &H80040E14 (-2147217900) in excel ADOB connection

I am trying to get the data from a SQL database through excel, I am using a ADOB connection. It was working fine and now i get a run time error, do not know what is the cause of it. I have not changed a code. My following code is:
Public Sub SQL_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim StrQuery As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
With ActiveSheet()
.Unprotect Password:=*****
End With
Sheets(2).Range("A2:H2").ClearContents
........
If CheckBox4.Value = True Then
strCon = "Provider=SQLOLEDB;Data Source=******\SQLEXPRESS;" & _
"Initial Catalog=testdata;" & _
"User ID=test;Password=*****;"
End If
........
If CheckBox4.Value = True Then
Const DB_CONNECT_STRING = "Provider=SQLOLEDB;Data Source=******\SQLEXPRESS;Initial Catalog=testdata;user id ='test';password=*****"
Set myConn = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command")
myConn.ConnectionTimeout = 15
myConn.Open DB_CONNECT_STRING1
Set myCommand.ActiveConnection = myConn
myCommand.CommandText = "UPDATE Rewind SET Cause = '" & Sheets(2).Range("I2") & "' WHERE RewindID = '" & Sheets(2).Range("J2") & "'"
myCommand.Execute
myConn.Close
End If
With ActiveSheet
.Protect Password:=66090
End With
End Sub
Please help.

How to make excel combobox.value triggering other textbox from access database

i have a combobox that have values from access database, how can i make the combobox as a trigger. When a value was chosen, then other textbox is automatically filled with corresponding value from access database? Thanks
On Error GoTo UserForm_Initialize_Err
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=E:\Database.accdb"
rs.Open "specification", cn, adOpenStatic
rs.MoveFirst
With Me.ComboBox1
.Clear
Do
.AddItem rs![SerialNoCubicle]
rs.MoveNext
Loop Until rs.EOF
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
Exit Sub
this code for calling the values from database displayed in combobox
UPDATE
i found the solution but my code is pretty noob, need someone to help me simplify the code if it is possible
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ace.OLEDB.12.0; " & _
"Data Source=E:\Database.accdb"
Set rs = New ADODB.Recordset
sql1 = "Select * FROM specification Where SerialNoCubicle = '8'"
sql2 = "Select * FROM specification Where SerialNoCubicle = '17'"
sql3 = "Select * FROM specification Where SerialNoCubicle = '18'"
If TextBox8.Value = 8 Then
rs.Open sql1, cn
With rs
TextBox1.Value = rs.Fields("Project").Value
TextBox2.Value = rs.Fields("ProjectNo").Value
TextBox3.Value = rs.Fields("No&DateofDrw").Value
TextBox4.Value = rs.Fields("DrawingNumber").Value
TextBox5.Value = rs.Fields("NameofCubicle").Value
TextBox6.Value = rs.Fields("SingleLineLayout").Value
TextBox7.Value = rs.Fields("PlantofTest").Value
TextBox9.Value = rs.Fields("TypeofProduct").Value
TextBox10.Value = rs.Fields("IPofProduct").Value
TextBox11.Value = rs.Fields("Substation").Value
End With
End If
If TextBox8.Value = 17 Then
rs.Open sql2, cn
With rs
TextBox1.Value = rs.Fields("Project").Value
TextBox2.Value = rs.Fields("ProjectNo").Value
TextBox3.Value = rs.Fields("No&DateofDrw").Value
TextBox4.Value = rs.Fields("DrawingNumber").Value
TextBox5.Value = rs.Fields("NameofCubicle").Value
TextBox6.Value = rs.Fields("SingleLineLayout").Value
TextBox7.Value = rs.Fields("PlantofTest").Value
TextBox9.Value = rs.Fields("TypeofProduct").Value
TextBox10.Value = rs.Fields("IPofProduct").Value
TextBox11.Value = rs.Fields("Substation").Value
End With
End If
If TextBox8.Value = 18 Then
rs.Open sql3, cn
With rs
TextBox1.Value = rs.Fields("Project").Value
TextBox2.Value = rs.Fields("ProjectNo").Value
TextBox3.Value = rs.Fields("No&DateofDrw").Value
TextBox4.Value = rs.Fields("DrawingNumber").Value
TextBox5.Value = rs.Fields("NameofCubicle").Value
TextBox6.Value = rs.Fields("SingleLineLayout").Value
TextBox7.Value = rs.Fields("PlantofTest").Value
TextBox9.Value = rs.Fields("TypeofProduct").Value
TextBox10.Value = rs.Fields("IPofProduct").Value
TextBox11.Value = rs.Fields("Substation").Value
End With
End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
use the _change event
Private Sub ComboBox1_Change()
'or create your own query
qry = "SELECT [FIELD1] FROM [TABLE] WHERE [FIELD2] = " & Me.ComboBox1.value & ";"
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=E:\Database.accdb"
rs.open qry, cn
Me.Textbox1.Value = rs![FIELD1]
'rinse and repeat for other text boxes
rs.close
cn.close
End Sub

Excel VBA LDAP query Network Printers from AD does not display PortName

I want to use the code below to quickly add all network printers from my domain into an Excel spreadsheet to use for my records. The code works fine except for the fact that the PortName (IP Address) is not displayed (cells are blank).
Could someone look over my code bellow and point out why is it not working for the PortName field..
Private Sub GetAllPrintersFromAD()
Const ADS_SCOPE_SUBTREE = 2
Set objRoot = GetObject("LDAP://rootDSE")
strDomain = objRoot.Get("defaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"SELECT distinguishedName,portName,location,servername FROM 'LDAP://" & strDomain & "' WHERE objectClass='printQueue'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
ActiveSheet.Range("A2").CopyFromRecordset objRecordSet
objRecordSet.Close
objConnection.Close
End Sub
1. Problem: Data types
Your code is not working for a few reasons:
The portName field is stored as DataTypeEnum 12 (Automation Variant: DBTYPE_VARIANT)
DBTYPE_VARIANT is unsupported for usage with ADO (source).
CopyFromRecordset has known data type issues (source)
Note: all other fields are stored as DataTypeEnum 202 (null-terminated Unicode character string).
2. Solution
You will need to iterate through the records and import the portName to a string, then write that string to the correct cell. This ensures that VBA handles the conversion, rather than CopyFromRecordset attempting to determine the (in)correct data type. If you would like to keep your original code with limited modification, I've provided a rudimentary example below.
I was able to duplicate your issue on my machine; the below modified code works as intended and includes the IP.
Private Sub GetAllPrintersFromAD()
Const ADS_SCOPE_SUBTREE = 2
Set objRoot = GetObject("LDAP://rootDSE")
strDomain = objRoot.Get("defaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"SELECT distinguishedName,portName,location,servername FROM 'LDAP://" & strDomain & "' WHERE objectClass='printQueue'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
ActiveSheet.Range("A2").CopyFromRecordset objRecordSet
'Copy over the portName field properly
objRecordSet.MoveFirst
i = 2
Do Until objRecordSet.EOF
strportname = vbNullString
On Error Resume Next
strportname = objRecordSet.Fields("portName")
Err.Clear
On Error GoTo 0
ActiveSheet.Range("B" & i).Value2 = strportname
i = i + 1
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
End Sub
I use this old script to write same data to .csv file. Works good for me. Give it a try.
'Query AD for Printer details form printer name
ReportLog = "OutPut.csv"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOut : Set objOut = objFSO.CreateTextFile(ReportLog)
objOut.WriteLine "Dis Name;printer name;port name;Location;Server name;"
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
strFilter = "(&(objectClass=printQueue))"
strAttributes = "distinguishedName,printShareName,portName,location,servername"
strQuery = strADsPath & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
'objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = "<ERROR>"
strPSN = "<ERROR>"
strPN = "<ERROR>"
strLO = "<ERROR>"
strSN = "<ERROR>"
On Error Resume Next
strDN = objRecordSet.Fields("distinguishedName")
strPSN = objRecordSet.Fields("printShareName")
strPN = objRecordSet.Fields("portName")
strLO = objRecordSet.Fields("location")
strSN = objRecordSet.Fields("serverName")
Err.Clear
On Error GoTo 0
objOut.WriteLine """" & strDN & """;""" & Join(strPSN, ";") & """;""" & Join(strPN, ";") & """;""" & strLO & """;""" & strSN & """"
objRecordSet.MoveNext
Loop
'Next
objOut.Close
WScript.Echo "Finished"
The output is:

MS Access 2010 using ldap authentication

I am trying to do ldap authentication in ms access 2010 using username and password. I cannot seem to figure this out and have tried different codes online but none seem to work. Can anyone help?
The following is what i have taken from here
Function CheckUser(username As String, passwd As String, Level As Integer) As Boolean
On Error GoTo LDAP_Error
username = "sharifu"
passwd = "xxx"
Const ADS_SCOPE_SUBTREE = 2
Dim LDAPPath As String
LDAPPath = "LDAP://172.16.0.12/OU=Sites;DC=domain;DC=com"
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
Set cmd = New ADODB.Command
conn.Provider = "ADsDSOObject"
conn.Properties("User ID") = "domain\" & username
conn.Properties("Password") = "" & passwd
conn.Properties("Encrypt Password") = True
'conn.Properties("ADSI Flag") = 3
conn.Open "Active Directory Provider"
Set cmd.ActiveConnection = conn
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
cmd.CommandText = _
"SELECT Name FROM '" & LDAPPath & "' WHERE " & "objectCategory='user'"
Set rs = cmd.Execute
rs.Close
conn.Close
CheckUser = True
Exit Function
LDAP_Error:
If Err.Number = -2147217911 Then
MsgBox "Incorrect PeopleSoftID or Password!", vbExclamation, "HILDA"
Else
MsgBox "Error : " & Err.Description & " " & Err.Number, vbExclamation, "HILDA"
End If
CheckUser = False
conn.Close
End Function
Error I receive is
"Error: The server is not operational.
-2147217865"
Changed to ip address get following error now
Method 'ActiveConnection' of object '_Command' failed but it might be coming from elsewhere in my code. how would i check if ldap was success?
I have fixed issue.
Function CheckUser(UserName As String, passwd As String, Level As Integer) As Boolean
On Error GoTo LDAP_Error
Const ADS_SCOPE_SUBTREE = 2
Dim LDAPPath As String
LDAPPath = "LDAP://akutan.country.domain.com/OU=Sites;DC=domain;DC=com"
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
Set cmd = New ADODB.Command
conn.Provider = "ADsDSOObject"
conn.Properties("User ID") = "xxx\" & UserName
conn.Properties("Password") = "" & passwd
conn.Properties("Encrypt Password") = True
'conn.Properties("ADSI Flag") = 3
conn.Open "Active Directory Provider"
Set cmd.ActiveConnection = conn
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
cmd.CommandText = "SELECT Name FROM '" & LDAPPath & "' WHERE " & "objectCategory='user'"
Set rs = cmd.Execute
rs.Close
conn.Close
CheckUser = True
[TempVars]![CurrentUser] = UserName
Call LogUser([TempVars]![CurrentUser], "Logon")
Exit Function
LDAP_Error:
If Err.Number = -2147217911 Then
MsgBox "Incorrect PeopleSoftID or Password!", vbExclamation, "LDAP Authentication"
Else
MsgBox "Error : " & Err.Description & " " & Err.Number, vbExclamation, "LDAP Authentication"
End If
CheckUser = False
conn.Close
End Function
Making little changes and explaining for understanding this code and a correct functioning:
Added the check if the user exist in the Database.
Changed "OU=Sites" in the LDAP path by "CN=Users".
LDAPPath = "LDAP://replace with IP or DNS name/CN=Users;DC=replace with domain name without .com;DC=replace with com, net or root node name"
In IP or DNS Name you must to specify the server IP or DNS Name.
In the first "DC" you must to specify the Domain Name without .com or .net would be like this "google".
In the second "DC" you must to specify the Domain type for intance "com", you can see this post if you want to know what means
Full example:
LDAPPath = "LDAP://200.201.1.1/CN=Users;DC=google;DC=com"
or
LDAPPath = "LDAP://ldap.google.com/CN=Users;DC=google;DC=com"
In this line: conn.Properties("User ID") = "replace with domain short name\" & userName
conn.Properties("User ID") = "ggle\" & userName
Finaly this the full code:
Function ldapAuth(userName As String, passwd As String, level As Integer) As Boolean
On Error GoTo LDAP_Error
ldapAuth = False
If Not IsNull(userName) And Not IsNull(passwd) Then
'Check if the user exist in DB
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As QueryDef
Dim strSQL As String
Set dbs = CurrentDb
strSelect = "SELECT *"
strFrom = " FROM employee"
strWhere = " WHERE user_name = '" & userName & "';"
strSQL = strSelect & strFrom & strWhere
Debug.Print strSQL
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
'If the recordset is empty, exit.
If rst.EOF Then
MsgBox "The user not exist in the DataBase!!!"
Else
'Check user with LDAP
Const ADS_SCOPE_SUBTREE = 2
Dim LDAPPath As String
LDAPPath = "LDAP://ldap.google.com/CN=Users;DC=google;DC=com"
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
Set cmd = New ADODB.Command
conn.Provider = "ADsDSOObject"
conn.Properties("User ID") = "ggle\" & userName
conn.Properties("Password") = "" & passwd
conn.Properties("Encrypt Password") = True
'conn.Properties("ADSI Flag") = 3
conn.Open "Active Directory Provider"
Set cmd.ActiveConnection = conn
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
cmd.CommandText = "SELECT Name FROM '" & LDAPPath & "' WHERE " & "objectCategory='user'"
Set rs = cmd.Execute
rs.Close
conn.Close
'Set userId and Role Globally
employeeId = rst![id]
employeeType = rst![employee_type]
TempVars.Add "employeeId", employeeId
TempVars.Add "employeeType", employeeType
'Log user login and role
Debug.Print "User login: " & TempVars!employeeId
Debug.Print "User Role: " & TempVars!employeeType
ldapAuth = True
rst.Close
End If
End If
Exit Function
LDAP_Error:
If Err.Number = -2147217911 Then
'MsgBox "Incorrect User or Password!", vbExclamation, "LDAP Authentication"
Else
MsgBox "Error : " & Err.Description & " " & Err.Number, vbExclamation, "LDAP Authentication"
End If
conn.Close
End Function