Re: VB.NET LDAP Class

From: Raterus (raterus_at_spam.org)
Date: 07/30/04


Date: Fri, 30 Jul 2004 12:31:08 -0400

This isn't the first time Joe has mentioned the faults in this code either, when I was trying to do what you are doing, I kept finding posts by him suggesting better ways, so I listened. Here is how I've been getting my groups after looking through all of his suggestions. It basically revolves around the use of tokenGroups.

I modified this too, for my purposes I needed a delimited string of groupnames. You also have to create the DirectoryEntry based on the user you are interested in, in the class I created I had already done that, so that is why you don't see "dn" declared, just used.

    Private Function GetGroups() As String
        Dim octetSid As String
        Dim binarySid() As Byte
        Dim binarySids As PropertyValueCollection
        Dim iterator As Integer
        Dim groups As StringBuilder = New StringBuilder

        Dim gEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & dn)
        gEntry.RefreshCache(New String() {"tokenGroups"})

        binarySids = gEntry.Properties("tokenGroups")
        For iterator = 0 To binarySids.Count - 1
            binarySid = CType(binarySids(iterator), Byte())
            octetSid = ConvertToOctetString(binarySid)

            Dim groupPath As String = String.Format("<SID={0}>", octetSid)
            Dim groupEntry As New DirectoryEntry("LDAP://" & groupPath)

            If iterator > 0 Then
                groups.Append("|")
            End If
            groups.Append(groupEntry.Properties("sAMAccountName").Value.ToString())

        Next

        Return groups.ToString

    End Function

    Private Function ConvertToOctetString(ByVal value As Byte()) As String
        Dim iterator As Integer
        Dim builder As System.Text.StringBuilder

        builder = New System.Text.StringBuilder((value.GetUpperBound(0) + 1) * 2)
        For iterator = 0 To value.GetUpperBound(0)
            builder.Append(value(iterator).ToString("x2"))
        Next

        Return builder.ToString()
    End Function

--Michael

"Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote in message news:OhFA1nkdEHA.712@TK2MSFTNGP09.phx.gbl...
> I wasn't criticizing your code Jon, I was criticizing the code in the
> article that Raterus pointed to when he suggested that you should have just
> used it as an example instead. That is a KBase article and needs to be held
> to a higher standard. It is a big pet peeve of mine.
>
> Your code is basically fine by me! Sorry for the confusion :)
>
> Joe K.



Relevant Pages

  • Re: VB.NET LDAP Class
    ... I wasn't criticizing your code Jon, I was criticizing the code in the ... >> group membership and does not include the primary group, ... >> Joe K. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: VB.NET LDAP Class
    ... This isn't the first time Joe has mentioned the faults in this code either, ... It basically revolves ... You also have to create the DirectoryEntry based on the user ... > I wasn't criticizing your code Jon, I was criticizing the code in the ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Hows NASCAR gonna call this finish?
    ... With cries of "all this confusion" etc., ... the next race, and Biffle has cashed his check... ... Before criticizing someone, walk a mile in their shoes. ...
    (rec.autos.sport.nascar)