Register | Login
Saturday, September 06, 2008

Sections
  
About Us
  
Hosting Provided by Server Intellect
Partners
Downloads
  
 WWWCoder.com Resource Directory

Getting User Groups from Active Directory
10/20/2005 10:10:56 AM

This code snippet provides you with a function to obtain all groups from within Active Directory using the System.DirectoryServices interface using VB.NET.

Summary: 

I needed a good way to get the groups for an active directory user, but in vb.net everything I saw was not quite what I needed or was in c# so I looked at an article available on MSDN, which is in c# and took out what I needed to get the groups for a certain user. There is one catch it will not return anything if there is not a "memberOf" attribute for a user. It actually errors out. so for this they are still a valid user they just are not in any other groups besides the "primary" group in AD so in the catch you can catch this and put in some code to handle these users. 

Setup:

1. In IIS go into Security and instead of using the default IIS username and password you have to add a valid username and password.

2. Add a reference to System.DirectoryServices and at the top of the codebehind add "Imports System.DirectoryServices" 

Code:  

Private Sub Page_Load(ByVal sender As System.Object, ByVal e _
    As System.EventArgs) Handles MyBase.Load
    Response.Write(GetGroups("LDAP://domainname", "username", "password"))
    'Returns String of: "Group1|Group2|Group3|"
End Sub
   
Private Function GetGroups(ByVal _path As String, ByVal _
     username As String, ByVal password As String) As String
    Dim GroupString As String
    Dim myDE As New System.DirectoryServices.DirectoryEntry(_path, _
      username, password)     

    Dim mySearcher As New DirectorySearcher(myDE)

    mySearcher.Filter = "sAMAccountName=" & username
    mySearcher.PropertiesToLoad.Add("memberOf")
    Dim propertyCount As Integer

    Try

        Dim myresult As SearchResult = mySearcher.FindOne()
        propertyCount = myresult.Properties("memberOf").Count
          
        Dim dn As String
        Dim equalsIndex, commaIndex As String

        For i As Integer = 0 To propertyCount - 1
            dn = myresult.Properties("memberOf")(i)
            equalsIndex = dn.IndexOf("=", 1)
            commaIndex = dn.IndexOf(",", 1)
            If equalsIndex = -1 Then
                Return Nothing
            End If

            GroupString += dn.Substring((equalsIndex + 1), _
              (commaIndex - equalsIndex) - 1) & "|"

        Next
        Return GroupString

    Catch ex As Exception
        If ex.GetType Is GetType(System.NullReferenceException) Then
            Response.Write("does not have a group")

            'they are still a good user just does not
            'have a "memberOf" attribute so it errors out.
            'code to do something else here if you want

        Else
            Response.Write(ex.Message.ToString & ex.ToString)
        End If 
    End Try 

 End Function

About the Author:

Charles Stratton has been programming since 1999 and enjoys staying on the edge of technology. his skills range from DNN to Sharepoint to Custom Development. currently working in VB.Net and C#.Net web and software. KISS - Programmers work smarter not harder. Charles can be reached at stratcr@peoplepc.com.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 4/18/2008 10:09:10 AM by Anonymous
Comments: Great thanks :)
Left on 8/7/2007 1:49:48 AM by Anonymous
Comments: Too wordy, use ad-advantage.net AD library for this
Left on 6/23/2007 3:23:05 PM by Anonymous
Comments:
No ratings available.
Left on 5/29/2007 8:27:57 AM by Anonymous
Comments: Excelente!
Left on 7/11/2006 3:40:55 PM by Anonymous
Comments: You're returning a pipe delimited string? That seems absurdly numb, why not return an array of strings?
Left on 3/17/2006 4:14:41 PM by Anonymous
Comments: vb.net allows it
No ratings available.
Left on 3/13/2006 5:47:52 PM by Anonymous
Comments: how can this work properly when you ...
Dim equalsIndex, commaIndex As String
and then those vars are subsequently used as integers in array ordinals?
just asking for a friend.
No ratings available.
Left on 2/14/2006 4:50:29 AM by Anonymous
Comments: Very good !

Left on 1/27/2006 11:11:30 AM by Anonymous
Comments: Dim equalsIndex, commaIndex As String
It better be Integer :-))
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970