Posted on

Windows AD Manager Code Snippet

 

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.DirectoryServices.AccountManagement;
  5. using System.DirectoryServices.ActiveDirectory;
  6.  
  7. namespace PB.ADUserManagement.Library
  8. {
  9.     public class ADManager
  10.     {
  11.         private ContextType _Context;
  12.  
  13.         public ADManager(ContextType context)
  14.         {
  15.             this._Context = context;
  16.             this.StartUp();
  17.         }
  18.  
  19.         public ADManager()
  20.         {
  21.             this._Context = ContextType.Domain;
  22.             this.StartUp();
  23.         }
  24.  
  25.         public static string CurrentDomain()
  26.         {
  27.             string name;
  28.             try
  29.             {
  30.                 name = Domain.GetCurrentDomain().Name;
  31.             }
  32.             catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
  33.             {
  34.                 name = Environment.MachineName;
  35.             }
  36.             catch (ActiveDirectoryOperationException activeDirectoryOperationException)
  37.             {
  38.                 name = Environment.MachineName;
  39.             }
  40.             return name;
  41.         }
  42.  
  43.         public ReadOnlyCollection<string> Groups()
  44.         {
  45.             PrincipalSearcher principalSearcher = new PrincipalSearcher()
  46.             {
  47.                 QueryFilter = new GroupPrincipal(new PrincipalContext(this._Context))
  48.             };
  49.             PrincipalSearchResult<Principal> principals = principalSearcher.FindAll();
  50.             List<string> strs = new List<string>();
  51.             foreach (Principal principal in principals)
  52.             {
  53.                 strs.Add(principal.SamAccountName);
  54.             }
  55.             strs.Sort();
  56.             return new ReadOnlyCollection<string>(strs);
  57.         }
  58.  
  59.         private static bool IsMemberOfDomain()
  60.         {
  61.             bool flag = false;
  62.             try
  63.             {
  64.                 Domain.GetComputerDomain();
  65.                 flag = true;
  66.             }
  67.             catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
  68.             {
  69.             }
  70.             return flag;
  71.         }
  72.  
  73.         private void StartUp()
  74.         {
  75.             if (!ADManager.IsMemberOfDomain())
  76.             {
  77.                 this._Context = ContextType.Machine;
  78.             }
  79.         }
  80.  
  81.         internal List<Principal> Users(string groupName)
  82.         {
  83.             PrincipalSearcher principalSearcher = new PrincipalSearcher();
  84.             GroupPrincipal groupPrincipal = new GroupPrincipal(new PrincipalContext(this._Context))
  85.             {
  86.                 SamAccountName = groupName
  87.             };
  88.             principalSearcher.QueryFilter = groupPrincipal;
  89.             GroupPrincipal groupPrincipal1 = (GroupPrincipal)principalSearcher.FindOne();
  90.             List<Principal> principals = new List<Principal>();
  91.             principals.AddRange(groupPrincipal1.Members);
  92.             return principals;
  93.         }
  94.     }
  95. }

 

High Level Usage is as follows;

var object = new ADManager();

cmbBox cmbBox.DataSource = object.Groups();