Introduction
I have written an application for deleting accounts from the Active Directory wrapper.
  1. public static string DelUserInAD(string sGroupName, string strDomain, string strName)
  2. {
  3. bool done = false;
  4. string result = string.Empty;
  5. try
  6. {
  7. PrincipalContext oPrincipalContext = GetPrincipalContext();
  8. string sUserName = strDomain + "\\" + strName;
  9. UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
  10. GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
  11. if (oUserPrincipal != null && oGroupPrincipal != null)
  12. {
  13. if (IsUserGroupMember(sUserName, sGroupName))
  14. {
  15. //oGroupPrincipal.Members.Add(oUserPrincipal);
  16. //oGroupPrincipal.Save();
  17. oGroupPrincipal.Members.Remove(oUserPrincipal);
  18. oGroupPrincipal.Save();
  19. done = !(oUserPrincipal.IsMemberOf(oGroupPrincipal));
  20. }
  21. else
  22. {
  23. result = sUserName + " already exists. Exiting!!";
  24. }
  25. }
  26. }
  27. catch
  28. {
  29. }
  30. return result;
  31. }

Comments

Join the conversation! Your thoughts help the community grow.