Newsgroup: it.comp.lang.visual-basic
Titolo: lista utenti con ldap

Salve,
con la funzione qui sotto sto cercando di ottenere le info di un utente di
un certo dominio.
in pratica vorrei autenticare l'utente loggato in windows.
il problema č che se alla funzione passo la login che si usa correttamente
per entrare in windows (M.Rossi) non mi restituisce nulla.
se invece passo il "Nome visualizzato" nell'active directory del server
(Mario Rossi) allora mi restituisce quello che desidero.
per quell'utente le informazioni contenute nell'active directory sono cosě
registrate :
Nome Mario
Cognome Rossi
Nome Visualizzato Mario Rossi
Nome accesso utente M.Rossi
dove sto sbagliando secondo voi ?
grazie mille
---------------------------------------------------------
Public Function UserInfo(LoginName As String) As String
'PURPOSE: Display information that is available in
'the Active Directory about a given user
'PARAMETER: Login Name for user
'RETURNS: String with selected information about
'user, or empty string if there is no such
'login on the current domain
'REQUIRES: Windows 2000 ADSI, LDAP Provider
'Proper Security Credentials.
'EXAMPLE: msgbox UserInfo("Administrator")
Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim sBase As String
Dim sFilter As String
Dim sDomain As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim sAns As String
Dim user As IADsUser
On Error GoTo ErrHandler:
'Get user Using LDAP/ADO.  There is an easier way
'to bind to a user object using the WinNT provider,
'but this way is a better for educational purposes
Set oRoot = GetObject("LDAP://rootDSE")
'work in the default domain
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
'Only get user name requested
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
  & LoginName & "))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
 
conn.Open _
  "Data Source=Active Directory Provider;Provider=ADsDSOObject"
 
Set rs = conn.Execute(sQuery)
If Not rs.EOF Then
    Set user = GetObject(rs("adsPath"))
    With user
 
    'if the attribute is not stored in AD,
    'an error will occur.  Therefore, this
    'will return data only from populated attributes
    On Error Resume Next
 
    sAns = "First Name: " & .FirstName & vbCrLf
    sAns = sAns & "Last Name " & .LastName & vbCrLf
    sAns = sAns & "Employee ID: " & .EmployeeID & vbCrLf
    sAns = sAns & "Title: " & .Title & vbCrLf
    sAns = sAns & "Division: " & .Division & vbCrLf
    sAns = sAns & "Department: " & .Department & vbCrLf
    sAns = sAns & "Manager: " & .Manager & vbCrLf
    sAns = sAns & "Phone Number: " & .TelephoneNumber & vbCrLf
    sAns = sAns & "Fax Number: " & .FaxNumber & vbCrLf
 
    sAns = sAns & "Email Address: " & .EmailAddress & vbCrLf
    sAns = sAns & "Web Page: " & .HomePage & vbCrLf
    sAns = sAns & "Last Login: " & .LastLogin & vbCrLf
    sAns = sAns & "Last Logoff: " & .LastLogoff & vbCrLf
 
    sAns = sAns & "Account Expiration Date: " _
         & .AccountExpirationDate & vbCrLf
 
    'IN RC2, this returned 1/1/1970 when password
    'never expires option is set
    sAns = sAns & "Password Expiration Date: " _
      & .PasswordExpirationDate
 
    End With
End If
UserInfo = sAns
rs.Close
Exit Function
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
    If rs.State <> 0 Then rs.Close
    Set rs = Nothing
End If
If Not conn Is Nothing Then
    If conn.State <> 0 Then conn.Close
    Set conn = Nothing
End If
Set oRoot = Nothing
Set oDomain = Nothing
End Function
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

 utente di un certo dominio.
Prova questo codice (creato con ScriptOMatic):
strContainer = ""
strName = "Administrator"
On Error Resume Next
'***********************************************
'*          Connect to an object                 *
'***********************************************
Set objRootDSE = GetObject("LDAP://rootDSE")
If strContainer = "" Then
  Set objItem = GetObject("LDAP://" & _
    objRootDSE.Get("defaultNamingContext"))
Else
  Set objItem = GetObject("LDAP://cn=" & strName & "," & strContainer & 
"," & _
    objRootDSE.Get("defaultNamingContext"))
End If
'***********************************************
'*         End connect to an object           *
'***********************************************
WScript.Echo VbCrLf & "** General Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strname = objItem.Get("name")
WScript.Echo "name: " & strname
strgivenName = objItem.Get("givenName")
WScript.Echo "givenName: " & strgivenName
strinitials = objItem.Get("initials")
WScript.Echo "initials: " & strinitials
strsn = objItem.Get("sn")
WScript.Echo "sn: " & strsn
strdisplayName = objItem.Get("displayName")
WScript.Echo "displayName: " & strdisplayName
strdescription = objItem.Get("description")
WScript.Echo "description: " & strdescription
strphysicalDeliveryOfficeName = objItem.Get("physicalDeliveryOfficeName")
WScript.Echo "physicalDeliveryOfficeName: " & 
strphysicalDeliveryOfficeName
strtelephoneNumber = objItem.Get("telephoneNumber")
WScript.Echo "telephoneNumber: " & strtelephoneNumber
strmail = objItem.Get("mail")
WScript.Echo "mail: " & strmail
strwWWHomePage = objItem.Get("wWWHomePage")
WScript.Echo "wWWHomePage: " & strwWWHomePage
WScript.Echo VbCrLf & "** General Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strotherTelephone = objItem.GetEx("otherTelephone")
WScript.Echo "otherTelephone:"
For Each Item in strotherTelephone
 WScript.Echo vbTab & Item
Next
strurl = objItem.GetEx("url")
WScript.Echo "url:"
For Each Item in strurl
 WScript.Echo vbTab & Item
Next
WScript.Echo VbCrLf & "** Address Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strstreetAddress = objItem.Get("streetAddress")
WScript.Echo "streetAddress: " & strstreetAddress
strl = objItem.Get("l")
WScript.Echo "l: " & strl
strst = objItem.Get("st")
WScript.Echo "st: " & strst
strpostalCode = objItem.Get("postalCode")
WScript.Echo "postalCode: " & strpostalCode
strc = objItem.Get("c")
WScript.Echo "c: " & strc
WScript.Echo VbCrLf & "** Address Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strpostOfficeBox = objItem.GetEx("postOfficeBox")
WScript.Echo "postOfficeBox:"
For Each Item in strpostOfficeBox
 WScript.Echo vbTab & Item
Next
WScript.Echo VbCrLf & "** Account Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
struserPrincipalName = objItem.Get("userPrincipalName")
WScript.Echo "userPrincipalName: " & struserPrincipalName
strdc = objItem.Get("dc")
WScript.Echo "dc: " & strdc
strsAMAccountName = objItem.Get("sAMAccountName")
WScript.Echo "sAMAccountName: " & strsAMAccountName
struserWorkstations = objItem.Get("userWorkstations")
WScript.Echo "userWorkstations: " & struserWorkstations
WScript.Echo VbCrLf & "** Account Properties Page**"
WScript.Echo "** (The userAccountControl attribute) **"
Set objHash = CreateObject("Scripting.Dictionary")
objHash.Add "ADS_UF_SMARTCARD_REQUIRED", &h40000
objHash.Add "ADS_UF_TRUSTED_FOR_DELEGATION", &h80000
objHash.Add "ADS_UF_NOT_DELEGATED", &h100000
objHash.Add "ADS_UF_USE_DES_KEY_ONLY", &h200000
objHash.Add "ADS_UF_DONT_REQUIRE_PREAUTH", &h400000
intuserAccountControl = objItem.Get("userAccountControl")
For Each Key in objHash.Keys
  If objHash(Key) And intuserAccountControl Then
    WScript.Echo Key & " is enabled."
  Else
    WScript.Echo Key & " is disabled."
  End If
Next
If objItem.IsAccountLocked = True Then
  WScript.Echo "ADS_UF_LOCKOUT is enabled"
Else
  WScript.Echo "ADS_UF_LOCKOUT is disabled"
End If
If err.Number = -
2147467259
 OR _
  objItem.AccountExpirationDate = "1/1/1970" Then
  WScript.Echo "Account doesn't expire."
Else
  WScript.Echo "Account expires on: " & objItem.AccountExpirationDate
End If
WScript.Echo VbCrLf & "** Profile Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strprofilePath = objItem.Get("profilePath")
WScript.Echo "profilePath: " & strprofilePath
strscriptPath = objItem.Get("scriptPath")
WScript.Echo "scriptPath: " & strscriptPath
strhomeDirectory = objItem.Get("homeDirectory")
WScript.Echo "homeDirectory: " & strhomeDirectory
strhomeDrive = objItem.Get("homeDrive")
WScript.Echo "homeDrive: " & strhomeDrive
WScript.Echo VbCrLf & "** Telephone Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strhomePhone = objItem.Get("homePhone")
WScript.Echo "homePhone: " & strhomePhone
strpager = objItem.Get("pager")
WScript.Echo "pager: " & strpager
strmobile = objItem.Get("mobile")
WScript.Echo "mobile: " & strmobile
strfacsimileTelephoneNumber = objItem.Get("facsimileTelephoneNumber")
WScript.Echo "facsimileTelephoneNumber: " & strfacsimileTelephoneNumber
stripPhone = objItem.Get("ipPhone")
WScript.Echo "ipPhone: " & stripPhone
strinfo = objItem.Get("info")
WScript.Echo "info: " & strinfo
WScript.Echo VbCrLf & "** Telephone Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strotherHomePhone = objItem.GetEx("otherHomePhone")
WScript.Echo "otherHomePhone:"
For Each Item in strotherHomePhone
 WScript.Echo vbTab & Item
Next
strotherPager = objItem.GetEx("otherPager")
WScript.Echo "otherPager:"
For Each Item in strotherPager
 WScript.Echo vbTab & Item
Next
strotherMobile = objItem.GetEx("otherMobile")
WScript.Echo "otherMobile:"
For Each Item in strotherMobile
 WScript.Echo vbTab & Item
Next
strotherFacsimileTelephoneNumber = 
objItem.GetEx("otherFacsimileTelephoneNumber")
WScript.Echo "otherFacsimileTelephoneNumber:"
For Each Item in strotherFacsimileTelephoneNumber
 WScript.Echo vbTab & Item
Next
strotherIpPhone = objItem.GetEx("otherIpPhone")
WScript.Echo "otherIpPhone:"
For Each Item in strotherIpPhone
 WScript.Echo vbTab & Item
Next
WScript.Echo VbCrLf & "** Organization Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strtitle = objItem.Get("title")
WScript.Echo "title: " & strtitle
strdepartment = objItem.Get("department")
WScript.Echo "department: " & strdepartment
strcompany = objItem.Get("company")
WScript.Echo "company: " & strcompany
strmanager = objItem.Get("manager")
WScript.Echo "manager: " & strmanager
WScript.Echo VbCrLf & "** Organization Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strdirectReports = objItem.GetEx("directReports")
WScript.Echo "directReports:"
For Each Item in strdirectReports
 WScript.Echo vbTab & Item
Next
WScript.Echo VbCrLf & "** Environment Properties Page**"
WScript.Echo "** (The ADSI Extension for Terminal Services interface) **"
WScript.Echo "TerminalServicesInitialProgram: " & _
  objItem.TerminalServicesInitialProgram
WScript.Echo "TerminalServicesWorkDirectory: " & _
  objItem.TerminalServicesWorkDirectory
WScript.Echo "ConnectClientDrivesAtLogon: " & _
  objItem.ConnectClientDrivesAtLogon
WScript.Echo "ConnectClientPrintersAtLogon: " & _
  objItem.ConnectClientPrintersAtLogon
WScript.Echo "DefaultToMainPrinter: " & _
  objItem.DefaultToMainPrinter
WScript.Echo VbCrLf & "** Sessions Properties Page**"
WScript.Echo "** (The ADSI Extension for Terminal Services interface) **"
WScript.Echo "MaxDisconnectionTime: " & _
  objItem.MaxDisconnectionTime
WScript.Echo "MaxConnectionTime: " & _
  objItem.MaxConnectionTime
WScript.Echo "MaxIdleTime: " & _
  objItem.MaxIdleTime
WScript.Echo "BrokenConnectionAction: " & _
  objItem.BrokenConnectionAction
WScript.Echo "ReconnectionAction: " & _
  objItem.ReconnectionAction
WScript.Echo VbCrLf & "** Remote Control Properties Page**"
WScript.Echo "** (The ADSI Extension for Terminal Services interface) **"
WScript.Echo "EnableRemoteControl: " & _
  objItem.EnableRemoteControl
Select Case objItem.EnableRemoteControl
  Case 0
    WScript.Echo "Remote Control disabled"
  Case 1
    WScript.Echo "Remote Control enabled"
    WScript.Echo "User permission required"
    WScript.Echo "Interact with the session"
  Case 2
    WScript.Echo "Remote Control enabled"
    WScript.Echo "User permission not required"
    WScript.Echo "Interact with the session"
  Case 3
    WScript.Echo "Remote Control enabled"
    WScript.Echo "User permission required"
    WScript.Echo "View the session"
  Case 4
    WScript.Echo "Remote Control enabled"
    WScript.Echo "User permission not required"
    WScript.Echo "View the session"
End Select
WScript.Echo VbCrLf & "** Terminal Services Profile Properties Page**"
WScript.Echo "** (The ADSI Extension for Terminal Services interface) **"
WScript.Echo "TerminalServicesProfilePath: " & _
  objItem.TerminalServicesProfilePath
WScript.Echo "TerminalServicesHomeDirectory: " & _
  objItem.TerminalServicesHomeDirectory
WScript.Echo "TerminalServicesHomeDrive: " & _
  objItem.TerminalServicesHomeDrive
WScript.Echo "AllowLogon: " & _
  objItem.AllowLogon
WScript.Echo VbCrLf & "** COM+ Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
WScript.Echo "msCOM-UserPartitionSetLink: "
WScript.Echo " " & objItem.Get("msCOM-UserPartitionSetLink")
WScript.Echo VbCrLf & "** Member Of Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strprimaryGroupID = objItem.Get("primaryGroupID")
WScript.Echo "primaryGroupID: " & strprimaryGroupID
WScript.Echo VbCrLf & "** Member Of Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strmemberOf = objItem.GetEx("memberOf")
WScript.Echo "memberOf:"
For Each Item in strmemberOf
 WScript.Echo vbTab & Item
Next
WScript.Echo VbCrLf & "** Object Properties Page**"
WScript.Echo "** (Single-Valued Attributes) **"
strwhenCreated = objItem.Get("whenCreated")
WScript.Echo "whenCreated: " & strwhenCreated
strwhenChanged = objItem.Get("whenChanged")
WScript.Echo "whenChanged: " & strwhenChanged
objItem.GetInfoEx Array("canonicalName"), 0
WScript.Echo VbCrLf & "** Object Properties Page**"
WScript.Echo "** (MultiValued Attributes) **"
strcanonicalName = objItem.GetEx("canonicalName")
WScript.Echo "canonicalName:"
For Each Item in strcanonicalName
 WScript.Echo vbTab & Item
Next
Ciao1
-- 
Gianluca Cannalire
Microsoft MVP/MCP - Visual Basic
http://www.visual-basic.it
http://community.visual-basic.it/gianluca

cosě come hai scritto tu non mi restituisce niente nemmeno administrator.
strContainer devo lasciarla vuota?
grazie


 strContainer devo lasciarla vuota?
Leggi qui:
http://www.microsoft.com/technet/images/scriptcenter/images/admatc04_big.gif
Ciao


allora ho fatto queste prove :
1)------------------------
strContainer = "cn=Users"
strName = "Administrator"
objItem viene aperto con questa istruzione
LDAP://cn=Administrator,cn=Users,DC=server,DC=local
OK
2)------------------------
strContainer = "cn=Users"
strName = "M.Rossi"
objItem viene aperto con questa istruzione
LDAP://cn=M.Rossi,cn=Users,DC=server,DC=local
KO (non da errori ma restituisce tutto vuoto tranne name)
3)------------------------
strContainer = "cn=Users"
strName = "Mario Rossi"
objItem viene aperto con questa istruzione
LDAP://cn=M.Rossi,cn=Users,DC=server,DC=local
KO 
non da errori ma restituisce tutto vuoto tranne name
il codice da me proposto con questo funzionava
grazie
ciao
