다음을 통해 공유


Main classes associated with Active Directory using C#

Introduction

The classes associated with the DirectoryEntry component can be used with any of the Active Directory Domain Services service providers. Some of the current providers are Internet Information Services (IIS), Lightweight Directory Access Protocol (LDAP), Novell NetWare Directory Service (NDS), and WinNT.

Justifying

You can use a DirectoryEntry or DirectorySearcher component instance when you want to interact with the contents of an Active Directory hierarchy in your application. I created some classes that can interact with the contents of an Active Directory hierarchy.

Entities



      public class  Administrator
                  {      
                      public string  LogonName { get; set; }  
                      public string  Password { get; set; }  
                  }      



      public class  User
                   {      
                       public string  FirstName { get; set; }  
                       public string  LastName { get; set; }  
                       public string  FullName { get; set; }  
                       public string  LogonName { get; set; }  
                       public string  Password { get; set; }  
                       public string  NewPassword { get; set; }  
                   }      
public class  General
    {
        public string  FirstName { get; set; }
        public string  LastName { get; set; }
        public string  Description { get; set; }
        public string  Office { get; set; }
        public string  TelephoneNumber { get; set; }
        public string  Email { get; set; }
        public string  WebPage { get; set; }
        public string  LogonName { get; set; }
        public string  DisplayName { get; set; }
        public string  Initials { get; set; }
    }



      public class  Address
                    {      
                        public string  Street { get; set; }  
                        public string  Pobox { get; set; }  
                        public string  City { get; set; }  
                        public string  StateProvince { get; set; }  
                        public string  ZipPostalCode { get; set; }  
                        public string  CountryRegionCode { get; set; }  
                        public string  LogonName { get; set; }  
              
                    }      



      public class  Telephones
                   {      
                       public string  Home { get; set; }  
                       public string  Pager { get; set; }  
                       public string  Mobile { get; set; }  
                       public string  Fax { get; set; }  
                       public string  IpPhone {get; set;}  
                       public string  Notes { get; set; }  
                       public string  LogonName { get; set; }  
                   }      
public class  Organization
  {
      public string  JobTitle { get; set; }
      public string  Department { get; set; }
      public string  Company { get; set; }
      public string  Manager { get; set; }
      public string  LogonName { get; set; }
  }



      public class  MemberOf
                   {      
                       public string  Name { get; set; }  
                       public string  logonName { get; set; }  
                   }      

Main Methods

public class  ActiveDirectoryResources
    {
        Controller.Controller control = new  Controller.Controller();
 
    public string  ChangePassword(string logonName, string password, string newPassord)
        {
            User user = new  User();
            user.LogonName = logonName;
            user.Password = password;
            user.NewPassword = newPassord;
            return control.ChangePassword(user);
              
        }
 
    public string  CreateUserAccount(string password, string firstName, string lastName, string userLogon, string logonAdmin, string passwordAdmin)
        {
            User user = new  User();
            Administrator admin = new  Administrator();
   
            user.LogonName = userLogon;
            user.FirstName = firstName;
            user.LastName = lastName;
            user.Password = password;
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
   
            return control.CreateUserAccount(user,admin);
        }
 
    public string  AddUserToOU(string  logonName, string  OU, string  logonAdmin, string  passwordAdmin)
        {
            User user = new  User();
            Administrator admin = new  Administrator();
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            user.LogonName = logonName;
            return control.AddUserToOU(user, admin, OU);
        }
 
    public string  AddUserToGroup(string logonName, string group, string logonAdmin, string passwordAdmin)
        {
            MemberOf member = new  MemberOf();
            Administrator admin = new  Administrator();
            member.logonName = logonName;
            member.Name = group;
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            return control.AddUserToGroup(member,admin);
              
        }
 
    public string  RemoveUserFromGroup(string logonName, string group, string logonAdmin, string passwordAdmin)
        {
            MemberOf member = new  MemberOf();
            Administrator admin = new  Administrator();
            member.logonName = logonName;
            member.Name = group;
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            return control.RemoveUserFromGroup(member,admin);
              
        }
 
    public string  GetUserGroupMembership(string logonName)
        {
            User user = new  User();
            user.LogonName = logonName;
            return control.GetUserGroupMembership(user);
        }
 
 
    public string  UpdateUserGeneral(string logonName, string firstName, string lastName, string description, string office, string email, string telephoneNumber ,string webPage, string logonAdmin, string passwordAdmin)
        {
           General user = new  General();
           Administrator admin = new  Administrator();
           admin.LogonName = logonAdmin;
           admin.Password = passwordAdmin;
              
           user.LogonName = logonName;
           user.FirstName = firstName;
           user.LastName = lastName;
           user.DisplayName = firstName + " " + lastName;
           user.Description = description;
           user.Email = email;
           user.TelephoneNumber = telephoneNumber;
           user.WebPage = webPage;
           user.Office = office;
           user.Initials = firstName.Substring(0,1) + lastName.Substring(0,1);
  
           return control.UpdateUserGeneral(user, admin);
       }
 
    ///<param name="countryRegionCode">
        ///     Field Country Region Code - Address
        ///     code="AQ" case "Antarctica
        ///     code="AG" case "Antigua and Barbuda
        ///     code="AR" case "Argentina
        ///     code="AM" case "Armenia
        ///     code="AW" case "Aruba
        ///     code="AU" case "Australia
        ///     code="AT" case "Austria
        ///     code="AZ" case "Azerbaijan
        ///     code="BS" case "Bahamas
        ///     code="BH" case "Bahrain
        ///     code="BD" case "Bangladesh
        ///     code="BB" case "Barbados
        ///     code="BY" case "Belarus
        ///     code="BE" case "Belgium
        ///     code="BZ" case "Belize
        ///     code="BJ" case "Benin
        ///     code="BM" case "Bermuda
        ///     code="BT" case "Bhutan
        ///     code="BO" case "Bolivia
        ///     code="BA" case "Bosnia and Herzegovina
        ///     code="BW" case "Botswana
        ///     code="BV" case "Bouvet Island
        ///     code="BR" case "Brazil
        ///     code="IO" case "British Indian Ocean Territory
        ///     code="BN" case "Brunei Darussalam
        ///     code="BG" case "Bulgaria
        ///     code="BF" case "Burkina Faso
        ///     code="BI" case "Burundi
        ///     code="KH" case "Cambodia
        ///     code="CM" case "Cameroon
        ///     code="CA" case "Canada
        ///     code="CV" case "Cape Verde
        ///     code="KY" case "Cayman Islands
        ///     code="CF" case "Central African Republic
        ///     code="TD" case "Chad
        ///     code="CL" case "Chile
        ///     code="CN" case "China
        ///     code="CX" case "Christmas Island
        ///     code="CC" case "Cocos (Keeling) Islands
        ///     code="CO" case "Colombia
        ///     code="KM" case "Comoros
        ///     code="CG" case "Congo
        ///     code="CD" case "Congo, The Democratic Republic of the
        ///     code="CK" case "Cook Islands
        ///     code="CR" case "Costa Rica
        ///     code="HR" case "Croatia
        ///     code="CY" case "Cyprus
        ///     code="CZ" case "Czech Republic
        ///     code="CI" case "Côte d'Ivoire
        ///     code="DK" case "Denmark
        ///     code="DJ" case "Djibouti
        ///     code="DM" case "Dominica
        ///     code="DO" case "Dominican Republic
        ///     code="EC" case "Ecuador
        ///     code="EG" case "Egypt
        ///     code="SV" case "El Salvador
        ///     code="GQ" case "Equatorial Guinea
        ///     code="ER" case "Eritrea
        ///     code="EE" case "Estonia
        ///     code="ET" case "Ethiopia
        ///     code="FK" case "Falkland Islands (Malvinas)
        ///     code="FO" case "Faroe Islands
        ///     code="FJ" case "Fiji
        ///     code="FI" case "Finland
        ///     code="FR" case "France
        ///     code="GF" case "French Guiana
        ///     code="PF" case "French Polynesia
        ///     code="TF" case "French Southern Territories
        ///     code="GA" case "Gabon
        ///     code="GM" case "Gambia
        ///     code="GE" case "Georgia
        ///     code="DE" case "Germany
        ///     code="GH" case "Ghana
        ///     code="GI" case "Gibraltar
        ///     code="GR" case "Greece
        ///     code="GL" case "Greenland
        ///     code="GD" case "Grenada
        ///     code="GP" case "Guadeloupe
        ///     code="GU" case "Guam
        ///     code="GT" case "Guatemala
        ///     code="GG" case "Guernsey
        ///     code="GN" case "Guinea
        ///     code="GW" case "Guinea-Bissau
        ///     code="GY" case "Guyana
        ///     code="HT" case "Haiti
        ///     code="HM" case "Heard Island and McDonald Islands
        ///     code="VA" case "Holy See (Vatican City State)
        ///     code="HN" case "Honduras
        ///     code="HK" case "Hong Kong
        ///     code="HU" case "Hungary
        ///     code="IS" case "Iceland
        ///     code="IN" case "India
        ///     code="ID" case "Indonesia
        ///     code="IQ" case "Iraq
        ///     code="IE" case "Ireland
        ///     code="IM" case "Isle of Man
        ///     code="IL" case "Israel
        ///     code="IT" case "Italy
        ///     code="JM" case "Jamaica
        ///     code="JP" case "Japan
        ///     code="JE" case "Jersey
        ///     code="JO" case "Jordan
        ///     code="KZ" case "Kazakhstan
        ///     code="KE" case "Kenya
        ///     code="KI" case "Kiribati
        ///     code="KR" case "Korea, Republic of
        ///     code="KW" case "Kuwait
        ///     code="KG" case "Kyrgyzstan
        ///     code="LA" case "Lao People's Democratic Republic
        ///     code="LV" case "Latvia
        ///     code="LB" case "Lebanon
        ///     code="LS" case "Lesotho
        ///     code="LR" case "Liberia
        ///     code="LY" case "Libyan Arab Jamahiriya
        ///     code="LI" case "Liechtenstein
        ///     code="LT" case "Lithuania
        ///     code="LU" case "Luxembourg
        ///     code="MO" case "Macao
        ///     code="MK" case "Macedonia, The former Yugoslav Republic of
        ///     code="MG" case "Madagascar
        ///     code="MW" case "Malawi
        ///     code="MY" case "Malaysia
        ///     code="MV" case "Maldives
        ///     code="ML" case "Mali
        ///     code="MT" case "Malta
        ///     code="MH" case "Marshall Islands
        ///     code="MQ" case "Martinique
        ///     code="MR" case "Mauritania
        ///     code="MU" case "Mauritius
        ///     code="YT" case "Mayotte
        ///     code="MX" case "Mexico
        ///     code="FM" case "Micronesia, Federated States of
        ///     code="MD" case "Moldova, Republic of
        ///     code="MC" case "Monaco
        ///     code="MN" case "Mongolia
        ///     code="ME" case "Montenegro
        ///     code="MS" case "Montserrat
        ///     code="MA" case "Morocco
        ///     code="MZ" case "Mozambique
        ///     code="MM" case "Myanmar
        ///     code="NA" case "Namibia
        ///     code="NR" case "Nauru
        ///     code="NP" case "Nepal
        ///     code="NL" case "Netherlands
        ///     code="AN" case "Netherlands Antilles
        ///     code="NC" case "New Caledonia
        ///     code="NZ" case "New Zealand
        ///     code="NI" case "Nicaragua
        ///     code="NE" case "Niger
        ///     code="NG" case "Nigeria
        ///     code="NU" case "Niue
        ///     code="NF" case "Norfolk Island
        ///     code="MP" case "Northern Mariana Islands
        ///     code="NO" case "Norway
        ///     code="OM" case "Oman
        ///     code="PK" case "Pakistan
        ///     code="PW" case "Palau
        ///     code="PS" case "Palestinian Territory
        ///     code="PA" case "Panama
        ///     code="PG" case "Papua New Guinea
        ///     code="PY" case "Paraguay
        ///     code="PE" case "Peru
        ///     code="PH" case "Philippines
        ///     code="PN" case "Pitcairn
        ///     code="PL" case "Poland
        ///     code="PT" case "Portugal
        ///     code="PR" case "Puerto Rico
        ///     code="QA" case "Qatar
        ///     code="RO" case "Romania
        ///     code="RU" case "Russian Federation
        ///     code="RW" case "Rwanda
        ///     code="RE" case "Réunion
        ///     code="BL" case "Saint Barthélemy
        ///     code="SH" case "Saint Helena
        ///     code="KN" case "Saint Kitts and Nevis
        ///     code="LC" case "Saint Lucia
        ///     code="MF" case "Saint Martin
        ///     code="PM" case "Saint Pierre and Miquelon
        ///     code="VC" case "Saint Vincent and the Grenadines
        ///     code="WS" case "Samoa
        ///     code="SM" case "San Marino
        ///     code="SA" case "Saudi Arabia
        ///     code="SN" case "Senegal
        ///     code="RS" case "Serbia
        ///     code="SC" case "Seychelles
        ///     code="SL" case "Sierra Leone
        ///     code="SG" case "Singapore
        ///     code="SK" case "Slovakia
        ///     code="SI" case "Slovenia
        ///     code="SB" case "Solomon Islands
        ///     code="SO" case "Somalia
        ///     code="ZA" case "South Africa
        ///     code="GS" case "South Georgia and the South Sandwich Islands
        ///     code="ES" case "Spain
        ///     code="LK" case "Sri Lanka
        ///     code="SR" case "Suriname
        ///     code="SJ" case "Svalbard and Jan Mayen
        ///     code="SZ" case "Swaziland
        ///     code="SE" case "Sweden
        ///     code="CH" case "Switzerland
        ///     code="ST" case "São Tome and Principe
        ///     code="TW" case "Taiwan
        ///     code="TJ" case "Tajikistan
        ///     code="TZ" case "Tanzania, United Republic of
        ///     code="TH" case "Thailand
        ///     code="TL" case "Timor-Leste
        ///     code="TG" case "Togo
        ///     code="TK" case "Tokelau
        ///     code="TO" case "Tonga
        ///     code="TT" case "Trinidad and Tobago
        ///     code="TN" case "Tunisia
        ///     code="TR" case "Turkey
        ///     code="TM" case "Turkmenistan
        ///     code="TC" case "Turks and Caicos Islands
        ///     code="TV" case "Tuvalu
        ///     code="UG" case "Uganda
        ///     code="UA" case "Ukraine
        ///     code="AE" case "United Arab Emirates
        ///     code="GB" case "United Kingdom
        ///     code="US" case "United States
        ///     code="UM" case "United States Minor Outlying Islands
        ///     code="UY" case "Uruguay code="UZ" case "Uzbekistan
        ///     code="VU" case "Vanuatu
        ///     code="VE" case "Venezuela
        ///     code="VN" case "Viet Nam
        ///     code="VG" case "Virgin Islands, British
        ///     code="VI" case "Virgin Islands, U.S.
        ///     code="WF" case "Wallis and Futuna
        ///     code="EH" case "Western Sahara
        ///     code="YE" case "Yemen
        ///     code="ZM" case "Zambia
        ///     code="ZW" case "Zimbabwe
        /// </param>
 
 
    public string  UpdateUserAddress(string logonName, string street, string poBox,string city, string stateProvince,string zipPostalCode, string countryRegionCode,string logonAdmin, string passwordAdmin)
        {
            Address user = new  Address();
            Administrator admin = new  Administrator();
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            user.LogonName = logonName;
            user.Street = street;
            user.Pobox = poBox;
            user.City = city;
            user.StateProvince = stateProvince;
            user.ZipPostalCode = zipPostalCode;
            user.CountryRegionCode = countryRegionCode;
   
            return control.UpdateUserAddress(user, admin);
        }
 
 
    public string  UpdateUserTelephones(string logonName, string home, string pager, string mobile, string fax, string ipPhone, string notes, string logonAdmin, string passwordAdmin)
        {
            Telephones user = new  Telephones();
            Administrator admin = new  Administrator();
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            user.LogonName = logonName;
            user.Home= home;
            user.Pager = pager;
            user.Mobile = mobile;
            user.Fax = fax;
            user.IpPhone = ipPhone;
            user.Notes = notes;
   
            return control.UpdateUserTelephones(user, admin);
   
        }
 
 
    public string  UpdateUserOrganization(string logonName, string jobTitle, string department, string company, string logonManager, string logonAdmin, string passwordAdmin)
        {
           Organization user = new  Organization();
           Administrator admin = new  Administrator();
           admin.LogonName = logonAdmin;
           admin.Password = passwordAdmin;
           user.LogonName = logonName;
           user.JobTitle = jobTitle;
           user.Department = department;
           user.Company = company;
           user.Manager = logonManager;
  
           return control.UpdateUserOrganization(user, admin);
  
       }
 
    public string  EnabledUserAccount(string logonName, string logonAdmin, string passwordAdmin, bool enabled)
        {
            Administrator admin = new  Administrator();
            User user = new  User();
            admin.LogonName = logonAdmin;
            admin.Password = passwordAdmin;
            user.LogonName = logonName;
   
            return control.EnabledUserAccount(user, admin, enabled);
        }
}

Main Class

public class  Controller
  {
   
      string domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName.ToString();
 
    public string  GetUserGroupMembership(User user)
        {
   
          ArrayList arrGroup = new  ArrayList();
   
          try
          {
              if (!Equals(domain, ""))
              {
                  DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain);
                  DirectorySearcher search = new  DirectorySearcher(entry, "(sAMAccountName=" + user.LogonName + ")");
                  SearchResult results = search.FindOne();
                  if (results != null)
                  {
                      DirectoryEntry obUser = new  DirectoryEntry(results.Path);
                      object obGroups = obUser.Invoke("Groups");
                      foreach (object ob in (IEnumerable)obGroups)
                      {
                          DirectoryEntry obGpEntry = new  DirectoryEntry(ob);
                          arrGroup.Add(obGpEntry.Name);
                      }
                      entry.Close();
                      return string.Join(",", (string[])arrGroup.ToArray(Type.GetType("System.String")));
                  }
                  else
                  {
                      return "User not found.";
                  }
              }
              else
              {
                  return "Domain not found.";
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
      }
 
    public string  AddUserToOU(User user, Administrator admin, string OU)
        {
          try
          {
              if (!Equals(user.LogonName, "") && !Equals(OU, "") && !Equals(admin.LogonName, "") && !Equals(admin.Password, ""))
              {
                  if (!Equals(domain, ""))
                  {
   
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher search = new  DirectorySearcher(entry);
                      search.Filter = "(&(objectCategory=organizationalUnit)(OU=" + OU + "))";
                      search.SearchRoot = entry;
                      SearchResult result1 = search.FindOne();
                      bool bResult = false;
                      string message = "";
   
                      if (result1 != null)
                      {
                          DirectoryEntry entyUser = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                          DirectorySearcher deSearch = new  DirectorySearcher(entry);
                          deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                          deSearch.SearchScope = SearchScope.Subtree;
                          SearchResult result2 = deSearch.FindOne();
   
                          if (result2 != null)
                          {
                              entyUser = result2.GetDirectoryEntry();
                              entry = result1.GetDirectoryEntry();
                              entyUser.MoveTo(entry);
                              entyUser.CommitChanges();
                              entyUser.Close();
                              entyUser.Dispose();
                              bResult = true;
                          }
                          else
                          {
                              bResult = false;
                              message = "User not found.";
                          }
                      }
                      else
                      {
                          bResult = false;
                          message = "Organization Unit not found.";
                      }
   
                      search.Dispose();
                      entry.Close();
                      entry.Dispose();
   
                      if (bResult)
                      {
                          return "ok";
                            
                      }
                      else
                      {
                          return message;
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform the user logon.";
                  }
                  else if  (Equals(OU, ""))
                  {
                      return "Please inform the Organization Unit(OU).";
   
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform the administrator logon.";
                  }
                  else
                  {
                      return "Please inform the administrator password";
                  }
              }
          }
          catch (Exception ex)
          {
              return "Error: " + ex.InnerException.Message.ToString();
          }
      }
 
    public string  AddUserToGroup(MemberOf member,Administrator admin)
        {
          try
          {
              if (!Equals(member.Name, "") && !Equals(member.logonName, "") && !Equals(admin.Password, "") && !Equals(admin.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher search = new  DirectorySearcher(entry);
                      search.SearchRoot = entry;
                      search.Filter = "(&(objectCategory=group)(CN="  + member.Name + "))";
                      SearchResult result = search.FindOne();
                      bool bResult = false;
                      string message = "";
   
                      if (result != null)
                      {
   
                          DirectoryEntry entyUser = result.GetDirectoryEntry();
                          //search = new DirectorySearcher(entyUser);
                          search.Filter = "(&(objectCategory=user)(CN="  + member.logonName + "))";
                          result = search.FindOne();
   
                          if (result != null)
                          {
   
                              DirectoryEntry user = result.GetDirectoryEntry();
                              entyUser.Invoke("Add", new  Object[] { user.Path });
                              entyUser.CommitChanges();
                              entyUser.Close();
                              entyUser.Dispose();
                              user.Close();
                              user.Dispose();
                              bResult = true;
                          }
                          else
                          {
                              bResult = false;
                              message = "User not found.";
                          }
                      }
                      else
                      {
                          bResult = false;
                          message = "Group not found.";
                      }
   
                      search.Dispose();
                      entry.Close();
                      entry.Dispose();
   
                      if (bResult)
                      {
                          return "ok";
                            
                      }
                      else
                      {
                          return message;
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(member.Name,""))
                  {
                      return "Please inform the Group (MemberOf).";
                  }
                  else if  (Equals(member.logonName))
                  {
                      return "Please inform the user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform the administrator logon.";
                  }
                  else
                  {
                      return "Please inform administrator password";
                  }
              }
          }
          catch (Exception ex)
          {
              if (!Equals(ex.InnerException.Message.IndexOf("0x80071392"), -1))
              {
                  return "the user " + member.logonName + " has been added into group "  + member.Name + ".";
              }
              else
              {
                  return "Error: " + ex.InnerException.Message.ToString();
   
              }
                 
          }
      }
 
    public string  RemoveUserFromGroup(MemberOf member, Administrator admin)
        {
          try
          {
              if (!Equals(member.Name, "") && !Equals(member.logonName, "") && !Equals(admin.Password, "") && !Equals(admin.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher search = new  DirectorySearcher(entry);
                      search.SearchRoot = entry;
                      search.Filter = "(&(objectCategory=group)(CN="  + member.Name + "))";
                      SearchResult result = search.FindOne();
                      bool bResult = false;
                      string message = "";
   
                      if (result != null)
                      {
   
                          DirectoryEntry entyUser = result.GetDirectoryEntry();
                          //search = new DirectorySearcher(entyUser);
                          search.Filter = "(&(objectCategory=user)(CN="  + member.logonName + "))";
                          result = search.FindOne();
   
                          if (result != null)
                          {
   
                              DirectoryEntry user = result.GetDirectoryEntry();
   
                              entyUser.Invoke("Remove", new  Object[] { user.Path });
   
                              entyUser.CommitChanges();
                              entyUser.Close();
                              entyUser.Dispose();
                              user.Close();
                              user.Dispose();
                              bResult = true;
                          }
                          else
                          {
                              bResult = false;
                              message = "The user belong the group "  + member.Name + ".";
                          }
                      }
                      else
                      {
                          bResult = false;
                          message = "Group not found.";
                      }
   
                      search.Dispose();
                      entry.Close();
                      entry.Dispose();
   
                      if (bResult)
                      {
                          return "ok";
                            
                      }
                      else
                      {
                          return message;
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(member.Name, ""))
                  {
                      return "Please inform the group (MemberOf).";
                  }
                  else if  (Equals(member.logonName))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon.";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
              }
          }
          catch (Exception ex)
          {
              if (!Equals(ex.InnerException.Message.IndexOf("0x80072035"), -1))
              {
                  return "the user "+ member.logonName + " has been removed the group "+ member.Name +".";
              }
              else
              {
                  return "Error: " + ex.InnerException.Message.ToString();
   
              }
          }
        }
 
    public string  EnabledUserAccount(User user,Administrator admin, bool enabled)
        {
          try
          {
              if (!Equals(admin.LogonName, "") && !Equals(admin.Password, "") && !Equals(user.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher deSearch = new  DirectorySearcher(entry);
                      deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                      deSearch.SearchScope = SearchScope.Subtree;
                      SearchResult result = deSearch.FindOne();
   
                      if (result != null)
                      {
                          entry = new  DirectoryEntry();
                          entry = result.GetDirectoryEntry();
   
                          if (enabled)
                          {
                              entry.Properties["userAccountControl"].Value = 512;
                          }
                          else
                          {
                              entry.Properties["userAccountControl"].Value = 514;
                          }
   
                          entry.CommitChanges();
   
                          entry.Close();
                          entry.Dispose();
                          return "ok";
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform Administrator logon";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
              }
   
          }
          catch(Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  UpdateUserOrganization(Organization user, Administrator admin)
        {
          try
          {
              if (!Equals(admin.LogonName, "") && !Equals(admin.Password, "") && !Equals(user.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher deSearch = new  DirectorySearcher(entry);
                      deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                      deSearch.SearchScope = SearchScope.Subtree;
                      SearchResult result = deSearch.FindOne();
   
                      if (result != null)
                      {
   
                          if (!Equals(user.Manager, ""))
                          {
   
                              deSearch = new  DirectorySearcher(entry);
                              deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.Manager + "))";
                              deSearch.SearchScope = SearchScope.Subtree;
                              SearchResult result2 = deSearch.FindOne();
                              if (Equals(result2, null))
                              {
                                  entry.Close();
                                  entry.Dispose();
                                  return "Manage Login (" + user.Manager + ") not found.";
                              }
                              else
                              {
                                  DirectoryEntry entryManager = new  DirectoryEntry();
                                  entryManager = result2.GetDirectoryEntry();
                                  user.Manager = entryManager.Properties["distinguishedName"].Value.ToString();
                              }
                          }
                             
                          entry = new  DirectoryEntry();
                          entry = result.GetDirectoryEntry();
                          entry.Properties["title"].Value = user.JobTitle;
                          entry.Properties["department"].Value = user.Department;
                          entry.Properties["company"].Value = user.Company;
   
                          if (!Equals(user.Manager, ""))
                          {
                              entry.Properties["manager"].Value = user.Manager;
                          }
   
                          entry.CommitChanges();
                          entry.Close();
                          entry.Dispose();
                          return "ok";
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon";
                  }
                  else
                  {
                      return "Please inform administrator password";
                  }
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  UpdateUserTelephones(Telephones user, Administrator admin)
        {
          try
          {
              if (!Equals(admin.LogonName, "") && !Equals(admin.Password, "") && !Equals(user.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                       DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher deSearch = new  DirectorySearcher(entry);
                      deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                      deSearch.SearchScope = SearchScope.Subtree;
                      SearchResult result = deSearch.FindOne();
   
                      if (result != null)
                      {
                          entry = new  DirectoryEntry();
                          entry = result.GetDirectoryEntry();
                          entry.Properties["homePhone"].Value = user.Home;
                          entry.Properties["pager"].Value = user.Pager;
                          entry.Properties["mobile"].Value=user.Mobile;
                          entry.Properties["facsimileTelephoneNumber"].Value = user.Fax;
                          entry.Properties["ipPhone"].Value = user.IpPhone;
                          entry.Properties["info"].Value = user.Notes;
                          entry.CommitChanges();
                          entry.Close();
                          entry.Dispose();
                          return "ok";
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon.";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  UpdateUserAddress(Address user, Administrator admin)
        {
          try
          {
              if (!Equals(admin.LogonName, "") && !Equals(admin.Password, "") && !Equals(user.LogonName, ""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher deSearch = new  DirectorySearcher(entry);
                      deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                      deSearch.SearchScope = SearchScope.Subtree;
                      SearchResult result = deSearch.FindOne();
   
                      if (result != null)
                      {
                          entry = new  DirectoryEntry();
                          entry = result.GetDirectoryEntry();
                          entry.Properties["streetAddress"].Value = user.Street;
                          entry.Properties["postOfficeBox"].Value = user.Pobox;
                          entry.Properties["l"].Value = user.City;
                          entry.Properties["st"].Value = user.StateProvince;
                          entry.Properties["postalCode"].Value = user.ZipPostalCode;
                          entry.Properties["c"].Value = user.CountryRegionCode;
                          entry.CommitChanges();
                          entry.Close();
                          entry.Dispose();
                          return "ok";
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon.";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  UpdateUserGeneral(General user, Administrator admin)
        {
          try
          {
              if (!Equals(admin.LogonName, "") && !Equals(admin.Password, "") && !Equals(user.LogonName,""))
              {
                  if (!Equals(domain, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                      DirectorySearcher deSearch = new  DirectorySearcher(entry);
                      deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                      deSearch.SearchScope = SearchScope.Subtree;
                      SearchResult result = deSearch.FindOne();
   
                      if (result != null)
                      {
                          entry = new  DirectoryEntry();
                          entry = result.GetDirectoryEntry();
                          entry.Properties["givenname"].Value = user.FirstName;
                          entry.Properties["displayname"].Value = user.DisplayName;
                          entry.Properties["description"].Value = user.Description;
                          entry.Properties["physicalDeliveryOfficeName"].Value = user.Office;
                          entry.Properties["mail"].Value = user.Email;
                          entry.Properties["telephoneNumber"].Value = user.TelephoneNumber;
                          entry.Properties["wwwHomePage"].Value = user.WebPage;
                          entry.Properties["initials"].Value = user.Initials;
   
                          entry.CommitChanges();
                          entry.Close();
                          entry.Dispose();
                          return "ok";
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      return "Domain not found.";
                  }
              }
              else
              {
                  if (Equals(user.LogonName, ""))
                  {
                      return "Please inform user logon.";
                  }
                  else if  (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon.";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  CreateUserAccount(User user,Administrator admin)
        {
          try
          {
              if (!Equals(admin.Password, "") && !Equals(admin.LogonName, ""))
              {
   
                  if (!Equals(user.Password, "") && !Equals(user.FirstName, "") && !Equals(user.LogonName, ""))
                  {
                      if (!Equals(domain, ""))
                      {
   
                          DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                          DirectorySearcher deSearch = new  DirectorySearcher(entry);
                          deSearch.Filter = "(&(objectClass=user)(SAMAccountName="  + user.LogonName + "))";
                          deSearch.SearchScope = SearchScope.Subtree;
                          SearchResult result = deSearch.FindOne();
   
                          if (Equals(result, null))
                          {
   
                              string oGUID = string.Empty;
                              entry = new  DirectoryEntry("LDAP://"  + domain, admin.LogonName, admin.Password, AuthenticationTypes.Secure);
                              DirectoryEntry newUser = entry.Children.Add("CN=" + user.LogonName, "user");
                              newUser.Properties["samAccountName"].Value = user.LogonName;
                              newUser.Properties["givenname"].Add(user.FirstName);
                              newUser.Properties["displayname"].Add(user.FirstName + " " + user.LastName);
                              newUser.Properties["SN"].Add(user.LastName);
                              newUser.Properties["userPrincipalName"].Add(user.LogonName.ToLower() + "@" + domain);
                              newUser.CommitChanges();
   
                              newUser.Invoke("SetPassword", new  object[] { user.Password });
                              newUser.CommitChanges();
                              oGUID = newUser.Guid.ToString();
                              entry.Close();
                              newUser.Close();
   
                              if (Equals(oGUID, ""))
                              {
                                  return "The user wasn't created with success.";
                              }
                              else
                              {
                                    
                                  return "ok";
                              }
                          }
                          else
                          {
                              return "There is an user with login "  + user.LogonName;
                          }
                      }
                      else
                      {
                          return "Domain not found.";
                      }
                  }
                  else
                  {
                      if (Equals(user.Password, ""))
                      {
                          return "Please inform the user password.";
                      }
                      else if  (Equals(user.FirstName))
                      {
                          return "Please inform the user first name.";
                      }
                      else
                      {
                          return "Please inform user logon.";
                      }
                  }
              }
              else
              {
                  if (Equals(admin.LogonName, ""))
                  {
                      return "Please inform administrator logon";
                  }
                  else
                  {
                      return "Please inform administrator password.";
                  }
   
              }
          }
          catch (Exception ex)
          {
              return ex.InnerException.Message.ToString();
          }
        }
 
    public string  ChangePassword(User user)
        {
          try
          {
              if (!Equals(domain, ""))
              {
                  if (!Equals(user.LogonName, "") && !Equals(user.Password, "") && !Equals(user.NewPassword, ""))
                  {
                      DirectoryEntry entry = new  DirectoryEntry("LDAP://"  + domain, user.LogonName, user.Password, AuthenticationTypes.Secure);
                      DirectorySearcher search = new  DirectorySearcher(entry);
                      search.Filter = "(SAMAccountName="  + user.LogonName + ")";
                      search.SearchScope = SearchScope.Subtree;
                      search.CacheResults = false;
                      SearchResultCollection results = search.FindAll();
                      string resultMessage = "";
   
                      if (results.Count > 0)
                      {
                          foreach (SearchResult result in results)
                          {
                              try
                              {
                                  entry = new  DirectoryEntry();
                                  entry = result.GetDirectoryEntry();
                                  entry.Invoke("ChangePassword", new  object[] { user.Password, user.NewPassword });
                                  entry.CommitChanges();
                                  //resultMessage = "Password changed with success.";
                                  resultMessage = "ok";
                                  entry.Close();
                              }
                              catch (Exception ex)
                              {
                                  if (!Equals(ex.InnerException.Message.IndexOf("0x800708C5"), -1))
                                  {
                                      resultMessage = "Password can not be changed due to the server restrictions. Check the minimum size required for password complexity required and password history requirements.";
                                  }
                                  else
                                  {
                                      resultMessage = "Password can not be changed due to the server restrictions. " + ex.InnerException.Message.ToString() + "";
                                  }
                              }
                          }
                          return resultMessage;
                      }
                      else
                      {
                          return "User not found.";
                      }
                  }
                  else
                  {
                      if (Equals(user.LogonName, ""))
                      {
                          return "Please inform user name.";
                      }
                      else if  (Equals(user.Password, ""))
                      {
                          return "Please inform actual user password.";
                      }
                      else
                      {
                          return "Please inform new password.";
                      }
                  }
              }
              else
              {
                  return "Domain not found.";
              }
          }
          catch (Exception ex)
          {
              return "Error: " + ex.InnerException.Message.ToString();
          }
        }
}

Conclusion

I hope this helps out all those programmers that had spent hours looking for the System.DirectoryServices command trying to seek answers on how to do AD tasks.