-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilityActiveDirectory.cs
421 lines (394 loc) · 13.6 KB
/
UtilityActiveDirectory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Text;
using System.Web;
namespace WordEngineering
{
///<summary>UtilityActiveDirectory</summary>
///<remarks>
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenumclasstopic.asp
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtsksearchingactivedirectoryhierarchy.asp
/// enterprise-minds.com Klaus Salchner
/// For a list of all WinNT ADSI objects, methods, properties, required properties and schema see following URLs
///
/// http://msdn.microsoft.com/library/en-us/adsi/adsi/adsi_objects_of_winnt.asp?frame=true
/// http://msdn.microsoft.com/library/en-us/adsi/adsi/winnt_object_class_hierarchy.asp?frame=true
/// http://msdn.microsoft.com/library/en-us/adsi/adsi/winnt_schemaampaposs_mandatory_and_optional_properties.asp?frame=true
///
/// For a list of all IIS ADSI objects, methods, properties, etc. see following URL
///
/// http://msdn.microsoft.com/library/en-us/iissdk/iis/ref_prog_iaoref.asp?frame=true
/// Provider
/// IIS://LocalHost
/// LDAP://LocalHost
/// NDS://LocalHost
/// NWCOMPAT://LocalHost
/// WinNT://LocalHost
///</remarks>
public class UtilityActiveDirectory
{
///<summary>ADSIProvider</summary>
public const string ADSIProvider = @"SOFTWARE\Microsoft\ADs\Providers";
///<summary>FormatPath</summary>
public const string FormatPath = "LDAP://{0}.com"; /* LDAP://DomainName.GenericTop-LevelDomains */
///<summary>FormatPropertyName</summary>
public const string FormatPropertyName = "{0}";
///<summary>FormatResultProperty</summary>
public const string FormatResultProperty = " {0}<br/>";
///<summary>FormatSearchResultPath</summary>
public const string FormatSearchResultPath = "Path: {0}<br/>";
///<summary>FullNameSchemaName</summary>
public const string FullNameSchemaName = "FullName";
///<summary>DirectoryListServicesAuthenticationTypes</summary>
public static List<string> ListDirectoryServicesAuthenticationTypes;
///<summary>SetPasswordMethodName</summary>
public const string SetPasswordMethodName = "SetPassword";
///<summary>TypeDirectoryServicesAuthentication</summary>
public static readonly Type TypeDirectoryServicesAuthentication = typeof(System.DirectoryServices.AuthenticationTypes);
///<summary>UserSchemaClassName</summary>
public const string UserSchemaClassName = "User";
///<summary>The entry point for the application.</summary>
///<param name="argv">Command-line parameters.</param>
public static void Main(string[] argv)
{
string exceptionMessage;
string filter = null;
string path = String.Format(FormatPath, Environment.UserDomainName);
StringBuilder sb;
if ( argv.Length > 0 ) { path = argv[0]; }
if ( argv.Length > 1 ) { filter = argv[1]; }
/*
DirectoryEntrySearcher( path, "guest", out sb, out exceptionMessage );
DirectoryEntrySearcher( path, null, out sb, out exceptionMessage );
string[] provider = GetListOfDirectoryProviders(null, out exceptionMessage);
*/
//AddUser("LDAP://EphraimTech.com","John Doe","John","J0hnD0e77aa6#@",out exceptionMessage);
/*
AddUser("WinNT://LocalHost","John Doe","JohnDoe","J0hnD0e",out exceptionMessage);
DeleteUser("WinNT://LocalHost", "JohnDoe", out exceptionMessage);
*/
DeleteUser("LDAP://EphraimTech.com", "JohnDoe", out exceptionMessage);
}
///<summary>AddDirectoryObject</summary>
public static void AddDirectoryObject
(
string path,
string objectName,
string objectSchemaClassName,
object[,] property,
object[,] method,
out string exceptionMessage
)
{
DirectoryEntry directoryEntryAdd = null;
DirectoryEntry directoryEntryPath = null;
exceptionMessage = null;
try
{
directoryEntryPath = new DirectoryEntry( path );
#if (DEBUG)
System.Console.WriteLine("path: {0}", path);
#endif
// creates the new Adsi object
directoryEntryAdd = directoryEntryPath.Children.Add(objectName, objectSchemaClassName);
// now loop through all the properties and set them
if ( property != null )
{
for ( int index = 0; index < property.GetLength(0); ++index )
{
directoryEntryAdd.Properties[Convert.ToString(property[index,0])].Value = property[index,1];
#if (DEBUG)
System.Console.WriteLine
(
"index: {0} | Convert.ToString(property[{0},0]): {1} | property[{0},1]: {2}",
index,
Convert.ToString(property[index,0]),
property[index,1]
);
#endif
}
}
if ( method != null )
{
for ( int index = 0; index < method.GetLength(0); ++index )
{
directoryEntryAdd.Invoke(Convert.ToString(method[index,0]), method[index,1]);
#if (DEBUG)
System.Console.WriteLine
(
"index: {0} | Convert.ToString(method[{0},0]): {1} | method[{0},1]: {2}",
index,
Convert.ToString(method[index,0]),
method[index,1]
);
#endif
}
}
//commit the changes
directoryEntryAdd.CommitChanges();
}
catch( Exception ex)
{
exceptionMessage = ex.Message;
}
finally
{
if ( directoryEntryAdd != null ) { directoryEntryAdd.Close(); }
if ( directoryEntryPath != null ) { directoryEntryPath.Close(); }
}
if ( exceptionMessage != null ) { System.Console.WriteLine( exceptionMessage ); }
}
///<summary>AddUser</summary>
///<remarks>AddUser("WinNT://LocalHost","JohnDoe","John","J0hnD0e",out exceptionMessage);</remarks>
public static void AddUser
(
string path,
string fullname,
string username,
string password,
out string exceptionMessage
)
{
object[,] property = new object[,] { { FullNameSchemaName, fullname } };
object[,] method = new object[,] { { SetPasswordMethodName, password } };
AddDirectoryObject( path, username, UserSchemaClassName, property, method, out exceptionMessage );
}
///<summary>DeleteDirectoryObject</summary>
public static void DeleteDirectoryObject
(
string path,
string objectName,
string objectSchemaClassName,
out string exceptionMessage
)
{
DirectoryEntry directoryEntryPath = null;
DirectoryEntry directoryEntryDelete = null;
exceptionMessage = null;
System.Console.WriteLine
(
"path: {0} | objectName: {1} | objectSchemaClassName: {2}",
path,
objectName,
objectSchemaClassName
);
try
{
directoryEntryPath = new DirectoryEntry(path);
if ( objectSchemaClassName != null )
{
directoryEntryDelete = directoryEntryPath.Children.Find(objectName, objectSchemaClassName);
}
else
{
directoryEntryDelete = directoryEntryPath.Children.Find(objectName);
}
if ( directoryEntryDelete != null )
{
directoryEntryDelete.Children.Remove(directoryEntryDelete);
}
}
catch( Exception ex)
{
exceptionMessage = ex.Message;
}
finally
{
if ( directoryEntryDelete != null ) { directoryEntryDelete.Close(); }
if ( directoryEntryPath != null ) { directoryEntryPath.Close(); }
}
if ( exceptionMessage != null ) { System.Console.WriteLine( exceptionMessage ); }
}
///<summary>DeleteUser</summary>
public static void DeleteUser(string path, string username, out string exceptionMessage)
{
DeleteDirectoryObject(path, username, UserSchemaClassName, out exceptionMessage);
}
///<summary>DirectoryEntrySearcher</summary>
///<remarks>
/// DirectoryEntrySearcher( ActiveDirectoryPath, "Comfort" );
/// DirectoryEntrySearcher( ActiveDirectoryPath, "guest" );
///</remarks>
public static SearchResultCollection DirectoryEntrySearcher
(
string path,
string filter,
out StringBuilder sb,
out String exceptionMessage
)
{
SearchResultCollection searchResultCollection;
searchResultCollection = DirectoryEntrySearcher
(
path,
null, //username
null, //password
filter,
out sb,
out exceptionMessage
);
return( searchResultCollection );
}
///<summary>DirectoryEntrySearcher</summary>
///<remarks>
/// DirectoryEntrySearcher( "LDAP://localhost", "host" );
/// DirectoryEntrySearcher( "LDAP://localhost", "Guest" );
/// DirectoryEntrySearcher( "IIS://localhost", null );
/// DirectoryEntrySearcher( "IIS://localhost/W3SVC", null );
/// DirectoryEntrySearcher( "WinNT://localhost", null );
///</remarks>
public static SearchResultCollection DirectoryEntrySearcher
(
string path,
string username,
string password,
string filter,
out StringBuilder sb,
out String exceptionMessage
)
{
DirectoryEntry directoryEntry = null;
DirectorySearcher directorySearcher = null;
ResultPropertyCollection resultPropertyCollection;
SearchResultCollection searchResultCollection = null;
sb = null;
exceptionMessage = null;
try
{
directoryEntry = new DirectoryEntry(path);
if ( string.IsNullOrEmpty(username) == false )
{
directoryEntry.Password = password;
directoryEntry.Username = username;
}
directorySearcher = new DirectorySearcher( directoryEntry );
if ( string.IsNullOrEmpty(filter) == false )
{
directorySearcher.Filter = (String.Format("(anr={0})", filter));
}
searchResultCollection = directorySearcher.FindAll();
sb = new StringBuilder();
foreach( SearchResult searchResult in searchResultCollection )
{
System.Console.WriteLine("Path: {0}", searchResult.GetDirectoryEntry().Path);
sb.AppendFormat( FormatSearchResultPath, searchResult.GetDirectoryEntry().Path );
resultPropertyCollection = searchResult.Properties;
foreach( string propertyName in resultPropertyCollection.PropertyNames )
{
System.Console.WriteLine("Property Name: {0}", propertyName);
sb.AppendFormat( FormatPropertyName, propertyName );
foreach( Object obj in resultPropertyCollection[propertyName] )
{
System.Console.WriteLine("\t {0}",obj);
sb.AppendFormat( FormatResultProperty, obj );
}
}
}
}
catch( Exception ex ) { exceptionMessage = ex.Message; }
finally
{
if (directorySearcher != null) {directorySearcher.Dispose();};
if (directoryEntry != null) {directoryEntry.Close();}
}
return( searchResultCollection );
}
///<summary>DirectoryEntrySearcherUser</summary>
public static SearchResultCollection DirectoryEntrySearcherUser()
{
Domain domain = Domain.GetCurrentDomain();
string domainName = domain.Name;
SearchResultCollection searchResultCollection = null;
using( DirectoryEntry root = new DirectoryEntry("LDAP://" + domainName) )
{
using( DirectorySearcher directorySearcher = new DirectorySearcher(root, "CN=Users"))
{
searchResultCollection = directorySearcher.FindAll();
}
}
return searchResultCollection;
}
///<summary>DirectoryEntryPropertySet</summary>
public static void DirectoryEntryPropertySet
(
string path,
string username,
string password,
out string exceptionMessage
)
{
System.DirectoryServices.DirectoryEntry directoryEntry;
exceptionMessage = null;
try
{
directoryEntry = new System.DirectoryServices.DirectoryEntry(path);
if ( string.IsNullOrEmpty(username) == false )
{
directoryEntry.Password = password;
directoryEntry.Username = username;
}
System.Console.WriteLine(directoryEntry.Properties["homenumber"].Count);
//directoryEntry.Properties["homenumber"][0]= "(425) 555-1212";
directoryEntry.CommitChanges();
}
catch ( Exception ex )
{
exceptionMessage = ex.Message;
}
if (exceptionMessage != null ) { System.Console.WriteLine(exceptionMessage); }
}
///<summary>GetListOfDirectoryProviders</summary>
///<remarks>
/// enterprise-minds.com Klaus Salchner
/// string[] provider = GetListOfDirectoryProviders("localhost", out exceptionMessage);
///</remarks>
public static string[] GetListOfDirectoryProviders
(
string computer,
out string exceptionMessage
)
{
string[] provider = null;
string[] subKey;
exceptionMessage = null;
if (computer == null) {computer = Environment.MachineName;}
try
{
subKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,computer).OpenSubKey(ADSIProvider).GetSubKeyNames();
// create the string array which will hold the provider list
provider = new string[subKey.Length];
// now add all providers to the array; all providers are
// pointed to the local machine
for (int count = 0; count < subKey.Length; ++count)
{
provider[count] = subKey[count] + "://" + computer;
System.Console.WriteLine( provider[count] );
}
}
catch( Exception ex)
{
exceptionMessage = ex.Message;
}
if ( exceptionMessage != null ) { System.Console.WriteLine(exceptionMessage); }
// return the list of providers
return (provider);
}
static UtilityActiveDirectory()
{
ListDirectoryServicesAuthenticationTypes = new List<string>();
foreach( string obj in Enum.GetNames( TypeDirectoryServicesAuthentication ) )
{
ListDirectoryServicesAuthenticationTypes.Add(obj);
}
/*
foreach( string listDirectoryServicesAuthenticationTypes in ListDirectoryServicesAuthenticationTypes )
{
System.Console.WriteLine( listDirectoryServicesAuthenticationTypes );
}
*/
}
}
}