farjack1
asked on
getting users againest LDAP group
i am trying to get LDAP group users but user are not retreaving i am putting code please look into it.
Public Function GetUsersForGroup() As ArrayList
Dim userNames As New ArrayList
Dim username = "******"
Dim pwd = "******"
Dim strLDAPPath As String
strLDAPPath = "LDAP://domain name"
Dim de As New DirectoryServices.Director yEntry(str LDAPPath) '<---make sure to change to your ad connstring
de.Username = username
de.Password = pwd '<--- domain account password
de.AuthenticationType = DirectoryServices.Authenti cationType s.None
Dim deSearch As New DirectoryServices.Director ySearcher( de)
Dim groupname As String = "groupname" '<---group you wish to load
deSearch.Filter = "(&(objectClass=group)(cn= " + groupname + "))"
Dim results As DirectoryServices.SearchRe sultCollec tion = deSearch.FindAll()
Dim result As DirectoryServices.SearchRe sult
If (results.Count > 0) Then
For Each result In results
For Each member As String In result.Properties("member" )
userNames.Add(member)
Next
Next
End If
Return userNames
End Function
please check and give me feedback.
Thanks
Public Function GetUsersForGroup() As ArrayList
Dim userNames As New ArrayList
Dim username = "******"
Dim pwd = "******"
Dim strLDAPPath As String
strLDAPPath = "LDAP://domain name"
Dim de As New DirectoryServices.Director
de.Username = username
de.Password = pwd '<--- domain account password
de.AuthenticationType = DirectoryServices.Authenti
Dim deSearch As New DirectoryServices.Director
Dim groupname As String = "groupname" '<---group you wish to load
deSearch.Filter = "(&(objectClass=group)(cn=
Dim results As DirectoryServices.SearchRe
Dim result As DirectoryServices.SearchRe
If (results.Count > 0) Then
For Each result In results
For Each member As String In result.Properties("member"
userNames.Add(member)
Next
Next
End If
Return userNames
End Function
please check and give me feedback.
Thanks
ASKER
no not exactly, basicaly groups are retreaving but not we required that are other, we have checked from LDAP guys they confirmed groups are on ldap the code of retreaving groups againest user is below, i don't know where is the problem in the code
Public Function GetUserGroups(ByVal UserName As String, ByVal Password As String, ByVal Environment As String) As List(Of String)
Dim i As Integer
Dim lineArray As Object
Dim m_GroupList As New List(Of String)
Dim entry As DirectoryEntry = New DirectoryEntry(strPathofLD AP)
entry.AuthenticationType = AuthenticationTypes.None
entry.Username = "uid=" & UserName & ",ou=people,dc=abc,dc=com"
entry.Password = Password
Dim search As DirectorySearcher = New DirectorySearcher(entry)
Dim _filterAttribute = getUserName(UserName)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad d("memberO f")
Dim groupNames As New System.Text.StringBuilder( )
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberO f").Count( )
Dim dn As String
Dim equalsIndex, commaIndex As Integer
Dim propertyCounter As Integer
For propertyCounter = 0 To propertyCount - 1
dn = result.Properties("memberO f")(proper tyCounter)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (-1 = equalsIndex) Then
groupNames.Append(dn)
Else
groupNames.Append(dn.Subst ring((equa lsIndex + 1), (commaIndex - equalsIndex) - 1))
groupNames.Append("|")
End If
Next propertyCounter
Catch ex As Exception
Throw New Exception("Error obtaining group names. " + ex.Message)
Finally
'entry.Dispose()
'entry = Nothing
search = Nothing
End Try
'Spliting group name in in lineArray
lineArray = Split(groupNames.ToString( ), "|")
'loop through on array and add in list
For i = 0 To UBound(lineArray) - 1
m_GroupList.Add(UCase(line Array(i)))
Next
Return m_GroupList
End Function
Public Function GetUserGroups(ByVal UserName As String, ByVal Password As String, ByVal Environment As String) As List(Of String)
Dim i As Integer
Dim lineArray As Object
Dim m_GroupList As New List(Of String)
Dim entry As DirectoryEntry = New DirectoryEntry(strPathofLD
entry.AuthenticationType = AuthenticationTypes.None
entry.Username = "uid=" & UserName & ",ou=people,dc=abc,dc=com"
entry.Password = Password
Dim search As DirectorySearcher = New DirectorySearcher(entry)
Dim _filterAttribute = getUserName(UserName)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad
Dim groupNames As New System.Text.StringBuilder(
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberO
Dim dn As String
Dim equalsIndex, commaIndex As Integer
Dim propertyCounter As Integer
For propertyCounter = 0 To propertyCount - 1
dn = result.Properties("memberO
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (-1 = equalsIndex) Then
groupNames.Append(dn)
Else
groupNames.Append(dn.Subst
groupNames.Append("|")
End If
Next propertyCounter
Catch ex As Exception
Throw New Exception("Error obtaining group names. " + ex.Message)
Finally
'entry.Dispose()
'entry = Nothing
search = Nothing
End Try
'Spliting group name in in lineArray
lineArray = Split(groupNames.ToString(
'loop through on array and add in list
For i = 0 To UBound(lineArray) - 1
m_GroupList.Add(UCase(line
Next
Return m_GroupList
End Function
ASKER
Hry Chris can you please responde?
Sorry... quite busy.
This won't work against Active Directory:
entry.Username = "uid=" & UserName & ",ou=people,dc=abc,dc=com"
You will find you won't be able to bind using UID=. Instead it would be:
entry.Username = "CN=Users Name,OU=people,DC=abc,DC=c
That makes getUserName a bit pointless as you already have the portion it retrieves as "Users Name".
Chris
ASKER
if i am defining like that
entry.Username = "CN=" & UserName & ",ou=people,dc=abc,dc=com"
its giving me error
as i am defining CN in filter
please suggest as per my above code
entry.Username = "CN=" & UserName & ",ou=people,dc=abc,dc=com"
its giving me error
as i am defining CN in filter
please suggest as per my above code
Is it giving you a invalid username and password error? That exception isn't handled by the Try / Catch above.
Chris
ASKER
now i handled exception like this, basically if put this line
entry.Username = "CN=" & UserName & ",ou=people,dc=ssga,dc=com "
ldap server is saying wrong uid and pass
Try
Dim entry As DirectoryEntry = New DirectoryEntry(strPath)
entry.AuthenticationType = AuthenticationTypes.None
entry.Username = "CN=" & UserName & ",ou=people,dc=ssga,dc=com "
entry.Password = Password
search = New DirectorySearcher(entry)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad d("memberO f")
result = search.FindOne()
propertyCount = result.Properties("memberO f").Count( )
Catch ex As Exception
End Try
but still have error object referance not set
entry.Username = "CN=" & UserName & ",ou=people,dc=ssga,dc=com
ldap server is saying wrong uid and pass
Try
Dim entry As DirectoryEntry = New DirectoryEntry(strPath)
entry.AuthenticationType = AuthenticationTypes.None
entry.Username = "CN=" & UserName & ",ou=people,dc=ssga,dc=com
entry.Password = Password
search = New DirectorySearcher(entry)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad
result = search.FindOne()
propertyCount = result.Properties("memberO
Catch ex As Exception
End Try
but still have error object referance not set
When you say "UserName" do you mean the "name" attribute or sAMAccountName (user logon name)? The latter will not work, it must be the name of the object as it appears in the directory (AD Users and Computers, ADSIEdit, etc).
Chris
ASKER
UserName means userid AD user, its not sAMAccountName
when i am putting
entry.Username = "uid=" & UserName & ",ou=people,dc=abc,dc=com"
its connecting on ldap and working but groups are not retreaving agaiest
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad d("memberO f")
result = search.FindOne()
when i am putting
entry.Username = "uid=" & UserName & ",ou=people,dc=abc,dc=com"
its connecting on ldap and working but groups are not retreaving agaiest
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Ad
result = search.FindOne()
ASKER
i am sorry Chris i am bothering again , but now it became urgent
ASKER
Chris i am still waiting of your response.
ASKER
Hi Chris can you please responde?
Hey,
Sorry for the late reply.
I think you should test the Directory Entry you've created. After all, if the DirectoryEntry fails then the search will fail (as the DE is used as the search base).
As a minimum the Directory Entry should have name and distinguishedName attributes which can be tested.
Chris
ASKER
can you send me code example.
Thanks
Thanks
This should be good enough:
Dim entry As DirectoryEntry = New DirectoryEntry(strPath)
entry.AuthenticationType = AuthenticationTypes.None
entry.Username = "CN=" & UserName & ",ou=people,dc=ssga,dc=com
entry.Password = Password
Label1.Text = entry.Properties("distingu
Obviously you should replace Label1.Text with something else that will help you see the value there.
Chris
ASKER
this is giving me error
'entry.Properties' is not declared or the module containing it is not loaded in the debugging session.
'entry.Properties' is not declared or the module containing it is not loaded in the debugging session.
Rather suggests that the DirectoryEntry failed. What value is being used in strPath?
Chris
ASKER
yes DirectoryEntry is getting failed and ldap path that is strPath is correct
LDAP://path/ou=people,dc=a bc,dc=com
LDAP://path/ou=people,dc=a
Okay, so either the user name and password used in the authentication string are incorrect, or the server / path are incorrect. Both of those should provide you with distinct error messages, but you don't get any?
Chris
ASKER
Chris today i will test this thing and will responde you.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey,
Have you confirmed that the search is returning the group correctly?
Chris