Question Ldap - Display Groups User Is Member Of in a ListBox

maximeus

New member
Joined
Oct 11, 2011
Messages
2
Programming Experience
Beginner
Hi,

I received this function to get groups a user is member of.

VB.NET:
[COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Function[/COLOR] GetRoles([COLOR=#0000ff]ByVal[/COLOR] user [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]) [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]()
 
        [COLOR=#0000ff]Dim[/COLOR] propertyCount [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR]
 
        [COLOR=#808080]'Initialisation du tableau avec 10 String "" [/COLOR]
        [COLOR=#0000ff]Dim[/COLOR] roles [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]()
        roles = [COLOR=#0000ff]New[/COLOR] [COLOR=#0000ff]String[/COLOR]([COLOR=#cc66cc]10[/COLOR]) {}
 
        [COLOR=#0000ff]Dim[/COLOR] myDE [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]New[/COLOR] System.DirectoryServices.DirectoryEntry([COLOR=#ff0000]"LDAP://NOMDOMAINE"[/COLOR], [COLOR=#ff0000]"userActiveDirectory"[/COLOR], [COLOR=#ff0000]"mdpActiveDirectory"[/COLOR])
        [COLOR=#0000ff]Dim[/COLOR] mySearcher [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]New[/COLOR] DirectorySearcher(myDE)
        mySearcher.Filter = [COLOR=#ff0000]"sAMAccountName="[/COLOR] & user
        mySearcher.PropertiesToLoad.Add([COLOR=#ff0000]"memberOf"[/COLOR])
 
        [COLOR=#0000ff]Try[/COLOR]
            [COLOR=#0000ff]Dim[/COLOR] myresult [COLOR=#0000ff]As[/COLOR] SearchResult = mySearcher.FindOne()
            propertyCount = myresult.Properties([COLOR=#ff0000]"memberOf"[/COLOR]).Count
 
            [COLOR=#0000ff]Dim[/COLOR] dn [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]
            [COLOR=#0000ff]Dim[/COLOR] equalsIndex, commaIndex [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]
            [COLOR=#0000ff]For[/COLOR] i [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR] = [COLOR=#cc66cc]0[/COLOR] [COLOR=#0000ff]To[/COLOR] propertyCount - [COLOR=#cc66cc]1[/COLOR]
                dn = myresult.Properties([COLOR=#ff0000]"memberOf"[/COLOR])(i).ToString
                equalsIndex = dn.IndexOf([COLOR=#ff0000]"="[/COLOR], [COLOR=#cc66cc]1[/COLOR]).ToString
                commaIndex = dn.IndexOf([COLOR=#ff0000]","[/COLOR], [COLOR=#cc66cc]1[/COLOR]).ToString
                [COLOR=#0000ff]If[/COLOR] equalsIndex = [COLOR=#ff0000]"-1"[/COLOR] [COLOR=#0000ff]Then[/COLOR]
                    [COLOR=#0000ff]Exit[/COLOR] [COLOR=#0000ff]For[/COLOR]
                [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
                roles(i) = dn.Substring(([COLOR=#0000ff]CInt[/COLOR](equalsIndex) + [COLOR=#cc66cc]1[/COLOR]), ([COLOR=#0000ff]CInt[/COLOR](commaIndex) - [COLOR=#0000ff]CInt[/COLOR](equalsIndex)) - [COLOR=#cc66cc]1[/COLOR])
            [COLOR=#0000ff]Next[/COLOR]
 
        [COLOR=#0000ff]Catch[/COLOR] ex [COLOR=#0000ff]As[/COLOR] Exception
 
            [COLOR=#0000ff]If[/COLOR] ex.[COLOR=#0000ff]GetType[/COLOR] [COLOR=#0000ff]Is[/COLOR] [COLOR=#0000ff]GetType[/COLOR](System.NullReferenceException) [COLOR=#0000ff]Then[/COLOR]
                [COLOR=#808080]'they are still a good user just does not [/COLOR]
                [COLOR=#808080]'have a "memberOf" attribute so it errors out.[/COLOR]
                [COLOR=#808080]'code to do something else here if you want[/COLOR]
                roles = [COLOR=#0000ff]Nothing[/COLOR]
            [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
        [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Try[/COLOR]
 
        [COLOR=#0000ff]Return[/COLOR] roles
 
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Function[/COLOR]

What I want is displaying the groups in a listbox when I click on button.

If I try with this code

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(GetRoles(TextBox1.Text))
    End Sub

, it displays only one line with a String[] Array value.

I need some help please.

Thank you.
 
Hi MattP,

Unfortunately I doesn't work

If I try with this code

VB.NET:
Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ListBox1.Items.AddRange(GetRoles(TextBox1.Text))

End Sub

the system show me this message : System.ArgumentNullException {"Value cannot be null. Parameter name: item"}

And if I try with

VB.NET:
Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ListBox1.Items.Add(GetRoles(TextBox1.Text))

End Sub

it shows me a String[]Array in my ListBox :apologetic:

Any idea?

Thanks
 
the system show me this message : System.ArgumentNullException {"Value cannot be null. Parameter name: item"}
That is because one of your string array items is a null reference (Nothing). Change GetRoles function so that is returns the proper results. You can redimension the array to exact number of results, or for example use a List(Of String) instead of array.
 

Latest posts

Back
Top