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.
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