Categories: Active Directory Posted on 5/21/2009 8:50 PM by Ryan Shelby  Feedback (4)

In this post I will talk about a VB.NET class that encapulates the Active Directory Group Object

 

This is part 3 of 4 posts I'm writing about using Active Directory Objects in .NET.  In part two I showed how to get the Active Directory User Object.  In part one I showed how to read security policies and password complexity properties directly from Active Directory, and then enforce them when allowing users to change their password online.

 

The Active Directory Group Object contains the following properties, however I'm sure you can extend this class to contain additional ones.

  • Group Name
  • Distinguished Name
  • Description
  • Users (an array of ADUser Objects)

 

The ADGroup Object contains 2 functions called LoadGroupsByUserName and LoadUsersByGroupName, both return an arraylist of AD Groups or AD Users.  The MemberOf property of the ADUser uses the LoadGroupsByUserName.

 

For example:  m_memberof = ADGroup.LoadGroupsByUserName(userName)

 

    1 Imports ActiveDirectory
    2 Imports System.Configuration
    3 Imports System.DirectoryServices
    4 
    5 Namespace ActiveDirectory
    6 
    7     Public Class ADGroup
    8 
    9         Private m_name As String 'cn
   10         Private m_distinguishedname As String
   11         Private m_description As String
   12         Private m_users As ArrayList
   13         Private m_ConnectionString As String = ConfigurationManager.ConnectionStrings("ADConnString").ConnectionString
   14 
   15 #Region "Public Properties"
   16 
   17         Public Property Name() As String
   18             Get
   19                 Return m_name
   20             End Get
   21             Set(ByVal Value As String)
   22                 m_name = Value
   23             End Set
   24         End Property
   25 
   26         Public Property DistinguishedName() As String
   27             Get
   28                 Return m_distinguishedname
   29             End Get
   30             Set(ByVal Value As String)
   31                 m_distinguishedname = Value
   32             End Set
   33         End Property
   34 
   35         Public Property Description() As String
   36             Get
   37                 Return m_description
   38             End Get
   39             Set(ByVal Value As String)
   40                 m_description = Value
   41             End Set
   42         End Property
   43 
   44         Public Property Users() As ArrayList
   45             Get
   46                 If m_users Is Nothing Then
   47                     m_users = LoadUsersByGroupName(DistinguishedName)
   48                 End If
   49                 Return m_users
   50             End Get
   51             Set(ByVal Value As ArrayList)
   52                 m_users = Value
   53             End Set
   54         End Property
   55 
   56 #End Region
   57 
   58         Private Function LoadUsersByGroupName(ByVal DistinguishedName As String) As ArrayList
   59 
   60             'Purpose: Returns an array of Active Directory Users belonging to a specific Active Directory Group (defined by DistinguishedName).
   61 
   62             Dim de1 As DirectoryEntry = ADManager.GetDirectoryObjectByDistinguishedName(DistinguishedName)
   63             Dim de2 As DirectoryEntry
   64             Dim ADUser As ADUser
   65             Dim list As New ArrayList
   66 
   67             For I As Integer = 0 To de1.Properties("member").Count - 1
   68                 de2 = ADManager.GetDirectoryObjectByDistinguishedName(m_ConnectionString & "/" & de1.Properties("member")(I).ToString())
   69 
   70                 Dim myPropertyName As String = ADManager.GetProperty(de2, "SAMAccountName")
   71 
   72                 If myPropertyName = "" Then 'User is a contact.
   73 
   74                     myPropertyName = ADManager.GetProperty(de2, "DisplayName")
   75                     ADUser = New ADUser(myPropertyName, "DisplayName")
   76                     list.Add(ADUser)
   77 
   78                 Else    'User is a regular user type.
   79 
   80                     ADUser = New ADUser(myPropertyName)
   81                     list.Add(ADUser)
   82 
   83                 End If
   84 
   85             Next I
   86 
   87             Return list
   88 
   89         End Function
   90 
   91         Friend Shared Function LoadGroupsByUserName(ByVal DistinguishedName As String) As ArrayList
   92 
   93             'Purpose: Returns an array of Groups an Active Directory User belongs to.
   94 
   95             Dim GroupEntries As DirectoryEntry = ADManager.GetDirectoryObjectByDistinguishedName(m_ConnectionString & "/" & DistinguishedName)
   96             Dim list As New ArrayList
   97             Dim de As DirectoryEntry
   98             Dim ADGroup As ADGroup
   99 
  100             For I As Integer = 0 To GroupEntries.Properties("memberOf").Count - 1
  101 
  102                 de = ADManager.GetDirectoryObjectByDistinguishedName(m_ConnectionString & "/" & GroupEntries.Properties("memberOf")(I).ToString())
  103 
  104                 Dim distName As String = ADManager.GetProperty(de, "DistinguishedName")
  105                 If distName.Contains("OU=Security Groups") Then
  106                     ADGroup = New ADGroup
  107                     ADGroup.Name = ADManager.GetProperty(de, "cn")
  108                     ADGroup.DistinguishedName = m_ConnectionString & "/" & distName
  109                     ADGroup.Description = ADManager.GetProperty(de, "Description")
  110                     list.Add(ADGroup)
  111                 End If
  112             Next I
  113 
  114             Return list
  115 
  116         End Function
  117 
  118     End Class
  119 
  120 End Namespace

 

The Active Directory Group Object depends on a 4th class called the ADManager. The ADManager helps setup various directory entries in order to load several directory objects related to ADGroup.  I will talk about this next!

 

Comments

wow leveling services
wow leveling services on 9/15/2009 7:49 AM Thanks for sharing this. Very informative. I'll be needing this for my thesis wed development project,since we'll be using vb.net. Looking forward for more updates.
Call center software
Call center software on 9/30/2009 8:53 AM I have follwed you and have extended the  Active Directory Group Object , Nice information
Otimização de Sites
Otimização de Sites on 10/5/2009 3:47 PM There are two types of Active Directory groups, each with a different purpose. These are:

    * Security principal groups - These objects can be assigned permissions and consist of:
          o users
          o groups
          o computers
    * Distribution groups - Used to group users for applications such as mail.

I´ve already participate and I do really enjoyed!
Thanks.
Multivariate testing
Multivariate testing on 10/6/2009 4:32 AM Thanks for the updated info as u said ,there are probably many more properties,but  the example that you have  shown how to encapsulate a user from Active Directory with  properties did the job for me,keep posting more

Send Feedback





biuquote
  • Comment
  • Preview
Loading