Link to home
Start Free TrialLog in
Avatar of Sabresfan
Sabresfan

asked on

LDAP Inquiry

Hi:

   I'm new to the VB world and have a question for some expert out there.  I have a form and what I'm trying to do is this.

Getting the info for user from AD. I want to pull the LDAP path.
 All I want is this. Put in the username in a textbox and a text field is populated with the LDAP sequence: cn=xxxx,cn=xxxx,dc=xxxx,dc=xxxx

I have researched it on the web but I'm not getting exactly what I want.  Seems simple enough but can't get there.  

Here's what I have already but I get errors.

Public Sub FindUserInfo(ByVal UserID As String)
            Dim domain As String = "LDAP://<domain.local>"
            Dim entry As System.DirectoryServices.DirectoryEntry = New DirectoryEntry(domain, <adsearcheraccnt>, <password>, AuthenticationTypes.Secure)

            If IsUserExistInActiveDirectory(UserID) Then
                Dim adSearcher As New DirectorySearcher(entry)
                adSearcher.SearchScope = SearchScope.Subtree
                adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + UserID + "))"
                Dim oResult As SearchResult = adSearcher.FindOne()

                DirectCast(DetailsView1.FindControl("lastname"), TextBox).Text = oResult.Properties("sn")

                DirectCast(DetailsView1.FindControl("firstname"), TextBox).Text = oResult.Properties("givenName")
                DirectCast(DetailsView1.FindControl("firstname"), TextBox).ReadOnly = True
            Else
                DirectCast(DetailsView1.FindControl("firstname"), TextBox).Text = "Username not found"
            End If

        End Sub
Any ideas or help wpuld be greatly appreciated.  Like I said all I want to do is pull LDAP information from AD. Pull the
Avatar of terencemcdermid
terencemcdermid
Flag of South Africa image

Hi, Try use this code to extract user information from AD, this code gets user information from AD using the user principal (display name) that it obtains from a form. Let me know if this is what you require.
'Declare vars
		'AD
		dim objConnection, objCommand, objRecordSet
 
		'Misc
		dim Username
 
	'Set Vars
		'AD
		Const ADS_SCOPE_SUBTREE = 2
		Set objConnection = CreateObject("ADODB.Connection")
		Set objCommand =   CreateObject("ADODB.Command")
		objConnection.Provider = "ADsDSOObject"
		objConnection.Open "Active Directory Provider"
		Set objCommand.ActiveConnection = objConnection
		
				
	'Get values from form
		Username = Request.Form("lstSelectUser")
		
	'Get data from AD
		objCommand.Properties("Page Size") = 1000
		objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
		objCommand.Properties("Sort On") = "Name"
 
		objCommand.CommandText = _
		    "SELECT Name, userPrincipalName, mail, telephonenumber, company FROM 'LDAP://ou=[OU_Name],dc=[DC_Name],dc=[DC_Name]' WHERE objectCategory='user' AND (userPrincipalName = '"& Username & "')"  
		Set objRecordSet = objCommand.Execute
 
		objRecordSet.MoveFirst

Open in new window

Avatar of Sabresfan
Sabresfan

ASKER

Sorry for the delay, couldn't get my way around on this board.

The 'get values from Form section -  The request.form is what exactly?  Just need some clarification.

Thanks.  
Hi,
Request.form is obtaining the user information from a previous form, you can subsitute it with whatever code you want to use to obtain your input from. I.E. textbox.text or listbox.selection
terencemcdermid,
     Go easy on me - I'm still a newbie to VB.  =)  
I try the code and here is what I get.  Conversion from string "lstSelectUser" to type 'Integer' is not valid.
I kind of understand but a little bit fuzzy.  The name that I enter in a textbox is a string, not an integer.  I don't understand what it is trying to convert.  

Thanks for all the help too.  I really appreciate it.  Many Thanks.
Hi could you give me your entire piece of code.. I'll go through it and have a look. Are you coding in VB.Net? or straight VB
coding in straight VB.  I used the code that was supplied.  
Could you supply me with your code?
Here's the code I had:
Public Sub FindUserInfo(ByVal UserID As String)
            Dim domain As String = "LDAP://<domain.local>"
            Dim entry As System.DirectoryServices.DirectoryEntry = New DirectoryEntry(domain, <adsearcheraccnt>, <password>, AuthenticationTypes.Secure)

            If IsUserExistInActiveDirectory(UserID) Then
                Dim adSearcher As New DirectorySearcher(entry)
                adSearcher.SearchScope = SearchScope.Subtree
                adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + UserID + "))"
                Dim oResult As SearchResult = adSearcher.FindOne()

                DirectCast(DetailsView1.FindControl("lastname"), TextBox).Text = oResult.Properties("sn")

                DirectCast(DetailsView1.FindControl("firstname"), TextBox).Text = oResult.Properties("givenName")
                DirectCast(DetailsView1.FindControl("firstname"), TextBox).ReadOnly = True
            Else
                DirectCast(DetailsView1.FindControl("firstname"), TextBox).Text = "Username not found"
            End If

        End Sub

Here is the code supplied:
'Declare vars
            'AD
            dim objConnection, objCommand, objRecordSet
 
            'Misc
            dim Username
 
      'Set Vars
            'AD
            Const ADS_SCOPE_SUBTREE = 2
            Set objConnection = CreateObject("ADODB.Connection")
            Set objCommand =   CreateObject("ADODB.Command")
            objConnection.Provider = "ADsDSOObject"
            objConnection.Open "Active Directory Provider"
            Set objCommand.ActiveConnection = objConnection
            
                        
      'Get values from form
            Username = Request.Form("lstSelectUser")
            
      'Get data from AD
            objCommand.Properties("Page Size") = 1000
            objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
            objCommand.Properties("Sort On") = "Name"
 
            objCommand.CommandText = _
                "SELECT Name, userPrincipalName, mail, telephonenumber, company FROM 'LDAP://ou=[OU_Name],dc=[DC_Name],dc=[DC_Name]' WHERE objectCategory='user' AND (userPrincipalName = '"& Username & "')"  
            Set objRecordSet = objCommand.Execute
 
            objRecordSet.MoveFirst
Hi, you need to substitute [OU_Name] and [DC_Name] in the following line with the correct info from your AD

objCommand.CommandText = _
                "SELECT Name, userPrincipalName, mail, telephonenumber, company FROM 'LDAP://ou=[OU_Name],dc=[DC_Name],dc=[DC_Name]' WHERE objectCategory='user' AND (userPrincipalName = '"& Username & "')"  
Well I did that and the error I'm getting is on the line where it says
'Get values from form
Username = Request.form("lstSelectUser")
I get an error that request isn't part of Ldap
Using VB 2008
OK I've treid the suggestions.  
The line objRecordSet = objCommand.Execute
is throwing an error.  

COM Exception was unhandled.  Table does not exist.   ALMOST THERE!  AHHHH !  
I'm becoming bald rather quickly.
Check the ErrorCode of the exception to determine the HRESULT returned by the COM object.  
My head hurts.  

ASKER CERTIFIED SOLUTION
Avatar of Sabresfan
Sabresfan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Sorry I haven't responded, have been away on vacation and didn't have access to a PC. I hope my information helped.
Terence,
 
     Is there a way to pull LDAP info from a textbox?  I know that the domain has to be declared if you will, [ i.e. Dim entry As New DirectoryEntry("LDAP://dc=xxx,dc=xxxx,dc=xxx")] but I was looking to further extend that and possibly have a textbox where I could enter the domain and userID, then click a command button and have it query AD to pull back the info. on the user (OU they are in), password expiration length, when the password was changed, and when the password expires.  
    Like passing the domain parameter to find the user information listed above.  I know vb has to use the % ...... % to pass variables but wanted just to pick your expertise to have an application that isn't limited to a single domain that needs to be entered in the code background, rather pass that as a parameter somehow so no matter what the domain is, it wouldn't be limited to the dimentions of entering the code.
Just a thought or maybe a challenge?  
"I finally resolved the issue..." Resolved it HOW?  I am having the same error (table not found) and have no idea why because I can get the OU_Name using VBScript but not in VB.NET.  It sure would be nice if someone posted the solution to the problem instead of just "never mind I figured it out."  Thanks in advance...