getting group information
-
Hello, I'm trying to get the group in which a user is sitting. I searched and found an artikle here. Converted it from c# to VB.NET. This is the code: Private Function GetADUserGroups(ByVal userName As String) As String Dim search As sys.DirectorySearcher = New sys.DirectorySearcher search.Filter = String.Format("(cn=*)", userName) search.PropertiesToLoad.Add("memberOf") Dim groupsList As System.Text.StringBuilder = New System.Text.StringBuilder groupsList.Append("test") result = search.FindOne() If Not result Is Nothing Then Dim groupCount As Integer groupCount = result.Properties.Count MessageBox.Show(groupCount) Dim counter As Integer For counter = 0 To groupCount - 1 Step counter + 1 groupsList.Append(CType(result.Properties("memberOf").Item(counter), String)) groupsList.Append("|") Next End If groupsList.Length -= 1 'remove the last "|" symbol Return groupsList.ToString() End Function if i try to run it i get a System.NullReferenceException in the for loop. does anyone know what the problem is? any other ideas are welcome. thanks in advance Bis
-
Hello, I'm trying to get the group in which a user is sitting. I searched and found an artikle here. Converted it from c# to VB.NET. This is the code: Private Function GetADUserGroups(ByVal userName As String) As String Dim search As sys.DirectorySearcher = New sys.DirectorySearcher search.Filter = String.Format("(cn=*)", userName) search.PropertiesToLoad.Add("memberOf") Dim groupsList As System.Text.StringBuilder = New System.Text.StringBuilder groupsList.Append("test") result = search.FindOne() If Not result Is Nothing Then Dim groupCount As Integer groupCount = result.Properties.Count MessageBox.Show(groupCount) Dim counter As Integer For counter = 0 To groupCount - 1 Step counter + 1 groupsList.Append(CType(result.Properties("memberOf").Item(counter), String)) groupsList.Append("|") Next End If groupsList.Length -= 1 'remove the last "|" symbol Return groupsList.ToString() End Function if i try to run it i get a System.NullReferenceException in the for loop. does anyone know what the problem is? any other ideas are welcome. thanks in advance Bis
Hi, I would like to give some suggestions so that i will work for you, as i have changed the same in my testing environment and it is working . . . Include the following line as the first line inside the function Dim entry As New DirectoryEntry("LDAP://") Change the second line as follows Dim search As DirectorySearcher = New DirectorySearcher(entry) include a if condition inside the for loop and it should appear like this
For counter = 0 To groupCount - 1 Step counter + 1 If result.Properties.Contains("memberOf") Then groupsList.Append(CType(result.Properties("memberOf").Item(counter), String)) groupsList.Append("|") End If Next i hope you would have declared result as search result Hope this will
-
Hi, I would like to give some suggestions so that i will work for you, as i have changed the same in my testing environment and it is working . . . Include the following line as the first line inside the function Dim entry As New DirectoryEntry("LDAP://") Change the second line as follows Dim search As DirectorySearcher = New DirectorySearcher(entry) include a if condition inside the for loop and it should appear like this
For counter = 0 To groupCount - 1 Step counter + 1 If result.Properties.Contains("memberOf") Then groupsList.Append(CType(result.Properties("memberOf").Item(counter), String)) groupsList.Append("|") End If Next i hope you would have declared result as search result Hope this will
-
Hi, I would like to give some suggestions so that i will work for you, as i have changed the same in my testing environment and it is working . . . Include the following line as the first line inside the function Dim entry As New DirectoryEntry("LDAP://") Change the second line as follows Dim search As DirectorySearcher = New DirectorySearcher(entry) include a if condition inside the for loop and it should appear like this
For counter = 0 To groupCount - 1 Step counter + 1 If result.Properties.Contains("memberOf") Then groupsList.Append(CType(result.Properties("memberOf").Item(counter), String)) groupsList.Append("|") End If Next i hope you would have declared result as search result Hope this will
-
Thanks for the reply, but still have some problems :( Made the changes you told me, but still doesn't work. Maybe it's the given username. I just give eg John. Is this correct or do you need to specify your domain as well? Bis
-
After some tests, i noticed that the code in the If condition never gets executed. So in other words, the property memberOf is not available. What can i do to get it available? Thanks in advance Kris