This will be my final post on Active Directory related objects. The ADManager Object is nothing special really. It's just an example showing how to retrieve all the Users, Groups, Security Groups, Distribution Groups or Contacts from Active Directory and then load them into an array list.
1 Imports System.DirectoryServices
2 Imports System.Configuration.ConfigurationManager
3
4 Namespace ActiveDirectory
5
6 Public Class ADManager
7
8 Private Shared m_instance As ADManager
9
10 'Active Directory Connection String, Username and Password stored in Config Manager in this example.
11 Private m_ADConnectionString As String = ConnectionStrings("ADConnString").ConnectionString
12 Private m_ADUser As String = AppSettings("ADUserName")
13 Private m_ADPassword As String = AppSettings("ADPassword")
14
15 Public Enum ADEntityType
16 Users = 1
17 Groups = 2
18 SecurityGroups = 3
19 DistributionGroups = 4
20 Contacts = 5
21 End Enum
22
23 #Region "Public Functions"
24
25 Public Function LoadUser(ByVal userName As String) As ADUser
26
27 'Loads a specific user defined by userName.
28
29 Dim myUser As New ADUser(userName)
30 Return myUser
31
32 End Function
33
34 Public Function LoadGroup(ByVal GroupName As String) As ADGroup
35
36 'Loads a specific Group defined by GroupName.
37
38 Dim de As DirectoryEntry = GetDirectoryObject()
39 Dim deSearch As DirectorySearcher = New DirectorySearcher
40 Dim ADGroup As New ADGroup
41
42 deSearch.SearchRoot = de
43 deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName + "))"
44 deSearch.SearchScope = SearchScope.Subtree
45 Dim results As SearchResult = deSearch.FindOne()
46
47 de = New DirectoryEntry(results.Path, m_ADUserName, m_ADPassword, AuthenticationTypes.Secure)
48
49 ADGroup.Name = ADManager.GetProperty(de, "cn")
50 ADGroup.DistinguishedName = m_ADConnectionString & "/" & ADManager.GetProperty(de, "DistinguishedName")
51 ADGroup.Description = ADManager.GetProperty(de, "Description")
52
53 Return ADGroup
54
55 End Function
56
57 Public Shared Function LoadEntities(ByVal ADType As ADEntityType) As ArrayList
58
59 'Purpose: Loads an entire collection of Users or Groups.
60 'ADEntityType can either be Users, Contacts, Groups, Distribution Groups or Security Groups (See Enum declared at top).
61
62 Dim de As DirectoryEntry = ADManager.GetDirectoryObject()
63 Dim de2 As DirectoryEntry
64 Dim deSearch As DirectorySearcher = New DirectorySearcher
65 Dim se As SearchResult
66 Dim list As New ArrayList
67 Dim newUser As ADUser
68 Dim ADGroup As ADGroup
69
70 deSearch.SearchRoot = de
71
72 If ADType = ADEntityType.Users Then
73
74 deSearch.Filter = "(&(objectClass=user)(objectCategory=person))"
75 deSearch.SearchScope = SearchScope.Subtree
76
77 For Each se In deSearch.FindAll
78 de2 = New DirectoryEntry(se.Path, m_ADUserName, m_ADPassword, AuthenticationTypes.Secure)
79 newUser = New ADUser(GetProperty(de2, "SAMAccountName"))
80 list.Add(newUser)
81 Next se
82
83 ElseIf ADType = ADEntityType.Contacts Then
84
85 deSearch.Filter = "objectClass=contact"
86 deSearch.SearchScope = SearchScope.Subtree
87 For Each se In deSearch.FindAll
88 de2 = New DirectoryEntry(se.Path, m_ADUserName, m_ADPassword, AuthenticationTypes.Secure)
89 newUser = New ADUser(GetProperty(de2, "SAMAccountName"))
90 list.Add(newUser)
91 Next se
92
93 ElseIf ADType = ADEntityType.Groups Then
94
95 deSearch.Filter = "objectClass=group"
96
97 For Each se In deSearch.FindAll
98 de2 = ADManager.GetDirectoryObjectByDistinguishedName(se.Path)
99 ADGroup = New ADGroup
100 ADGroup.Name = GetProperty(de2, "cn")
101 ADGroup.DistinguishedName = se.Path
102 ADGroup.Description = GetProperty(de2, "Description")
103 list.Add(ADGroup)
104 Next se
105
106 ElseIf ADType = ADEntityType.SecurityGroups Then
107
108 deSearch.Filter = "objectClass=group"
109
110 For Each se In deSearch.FindAll
111
112 If se.Path.Contains("OU=Security Groups") Then
113 de2 = ADManager.GetDirectoryObjectByDistinguishedName(se.Path)
114 ADGroup = New ADGroup
115 ADGroup.Name = GetProperty(de2, "cn")
116 ADGroup.DistinguishedName = se.Path
117 ADGroup.Description = GetProperty(de2, "Description")
118 End If
119
120 Next se
121
122 Else 'ADEntityType.DistributionGroups
123
124 deSearch.Filter = "objectClass=group"
125
126 For Each se In deSearch.FindAll
127
128 If se.Path.Contains("OU=Distribution Groups") Then
129 de2 = ADManager.GetDirectoryObjectByDistinguishedName(se.Path)
130 ADGroup = New ADGroup
131 ADGroup.Name = GetProperty(de2, "cn")
132 ADGroup.DistinguishedName = se.Path
133 ADGroup.Description = GetProperty(de2, "Description")
134 list.Add(ADGroup)
135 End If
136
137 Next se
138
139 End If
140
141 Return list
142
143 End Function
144
145 #End Region
146
147 #Region "Friend Functions"
148
149 Friend Shared Function GetDirectoryObjectByDistinguishedName(ByVal ObjectPath As String) As DirectoryEntry
150 Dim oDE As DirectoryEntry = New DirectoryEntry(ObjectPath, m_ADUserName, m_ADPassword, AuthenticationTypes.Secure)
151 Return oDE
152 End Function
153
154 Friend Shared Function GetDirectoryObject() As DirectoryEntry
155 Dim oDE As DirectoryEntry = New DirectoryEntry( _
156 m_ADConnectionString, m_ADUserName, m_ADPassword, AuthenticationTypes.Secure)
157 Return oDE
158 End Function
159
160 Friend Shared Function GetProperty(ByVal oDE As DirectoryEntry, ByVal PropertyName As String) As String
161 If oDE.Properties.Contains(PropertyName) Then
162 Return oDE.Properties(PropertyName)(0).ToString()
163 Else
164 Return String.Empty
165 End If
166 End Function
167
168 #End Region
169
170 End Class
171
172 End Namespace