Introduction
I have written an application for deleting accounts from the Active Directory wrapper.
- public static string DelUserInAD(string sGroupName, string strDomain, string strName)
- {
- bool done = false;
-
- string result = string.Empty;
- try
- {
- PrincipalContext oPrincipalContext = GetPrincipalContext();
- string sUserName = strDomain + "\\" + strName;
- UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
- GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
- if (oUserPrincipal != null && oGroupPrincipal != null)
- {
- if (IsUserGroupMember(sUserName, sGroupName))
- {
-
-
- oGroupPrincipal.Members.Remove(oUserPrincipal);
- oGroupPrincipal.Save();
- done = !(oUserPrincipal.IsMemberOf(oGroupPrincipal));
-
- }
- else
- {
- result = sUserName + " already exists. Exiting!!";
- }
- }
-
- }
- catch
- {
-
- }
- return result;
- }