public static void DeleteGroup(string identity, bool dryRun = false) { if (String.IsNullOrWhiteSpace(identity)) { throw new AdException("Group identity is not specified.", AdStatusType.MissingInput); } try { String id = null; String domain = GetDomain(identity, out id); GroupPrincipal groupPrincipal = GetGroupPrincipal(id, domain); if (groupPrincipal != null) { if (!dryRun) { groupPrincipal.Delete(); } } else { throw new AdException($"Group [{identity}] cannot be found.", AdStatusType.DoesNotExist); } } catch (InvalidOperationException e) { throw e; } }
public void DeleteThisGroup() { PrincipalContext pc = new PrincipalContext(ContextType.Domain); GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, IdentityType.Name, Name); gp.Delete(); }
public void Delete(string groupName) { GroupPrincipal gp = null; try { if (string.IsNullOrEmpty(groupName)) { throw new MissingFieldException("Users", "groupName"); } log.DebugFormat("Attempting to delete group {0}", groupName); pc = GetPrincipalContext(); gp = GroupPrincipal.FindByIdentity(pc, IdentityType.Name, groupName); if (gp == null) { throw new NoMatchingPrincipalException(groupName); } gp.Delete(); } catch (Exception ex) { log.ErrorFormat("Error deleting group {0}. Exception: {1}", groupName, ex.ToString()); throw; } finally { if (gp != null) { gp.Dispose(); } } }
public void TestNegativeCases() { UserData u1 = UserData.GenerateUserData("CoreFxUser6"); GroupData g1 = GroupData.GenerateGroupData("CoreFXGroup6"); DeleteUser(u1.Name); DeleteGroup(g1.Name); try { Assert.Throws <InvalidEnumArgumentException>(() => new PrincipalContext((ContextType)768, null, null, null)); Assert.Throws <PrincipalServerDownException>(() => new PrincipalContext(ContextType.Domain, "InvalidDomainName", null, null)); Assert.Throws <ArgumentException>(() => new PrincipalContext(ContextType.Domain, LdapConfiguration.Configuration.ServerName, "InvalidTestUserName", null)); Assert.Throws <ArgumentException>(() => new PrincipalContext(ContextType.Domain, LdapConfiguration.Configuration.ServerName, LdapConfiguration.Configuration.UserName, null)); Assert.Throws <ArgumentException>(() => new UserPrincipal(null)); Assert.Throws <ArgumentException>(() => new GroupPrincipal(null)); using (PrincipalContext context = DomainContext) { using (UserPrincipal user = CreateUser(context, u1)) using (GroupPrincipal group = CreateGroup(context, g1)) { Assert.Throws <PrincipalExistsException>(() => CreateUser(context, u1)); Assert.Throws <PrincipalExistsException>(() => CreateGroup(context, g1)); group.Members.Add(context, IdentityType.Name, user.Name); group.Save(); Assert.Throws <PrincipalExistsException>(() => group.Members.Add(context, IdentityType.Name, user.Name)); group.Members.Remove(context, IdentityType.Name, user.Name); group.Save(); user.Delete(); Assert.Throws <InvalidOperationException>(() => user.Delete()); Assert.Throws <InvalidOperationException>(() => user.Save()); group.Delete(); Assert.Throws <InvalidOperationException>(() => group.Delete()); Assert.Throws <InvalidOperationException>(() => group.Save()); } } } finally { DeleteUser(u1.Name); DeleteGroup(g1.Name); } }
/// <summary> /// Elimina un grupo /// </summary> /// <param name="nombre">Nombre del grupo a eliminar</param> public void EliminarGrupo(string nombre) { GroupPrincipal grupo = BuscarGrupo(nombre); if (grupo != null) { grupo.Delete(); } }
private void DeleteLocalGroup(string name) { using (PrincipalContext context = new PrincipalContext(ContextType.Machine)) { using (GroupPrincipal g = GroupPrincipal.FindByIdentity(context, name)) { g?.Delete(); } } }
/// <summary> /// Deletes the specified group. /// </summary> /// <param name="name">The unique identifier of the group to delete.</param> /// <returns>True if the group was deleted, false otherwise.</returns> public static Boolean DeleteGroup(string name) { try { GroupPrincipal g = GroupPrincipal.FindByIdentity(GetPrincipalContext(), name); g.Delete(); g.Save(); return(true); } catch { return(false); } }
public void DeleteGroup(string systemRoleIdentifier, string itSystemIdentifier) { string contextPath = "OU=" + itSystemIdentifier + "," + groupOU; using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, contextPath)) { using (GroupPrincipal group = GroupPrincipal.FindByIdentity(context, systemRoleIdentifier)) { if (group != null) { group.Delete(); log.Info("Deleted security group: " + systemRoleIdentifier + " in " + contextPath); } } } }
private void DeleteGroup(string groupName) { try { using (PrincipalContext context = DomainContext) using (GroupPrincipal p = FindGroup(groupName, context)) { if (p != null) { p.Delete(); } } } catch { // ignore the failure as we use this method to ensure clean up even if the group not exist } }
public void TestDeleteUserAndGroup() { UserData u1 = UserData.GenerateUserData("CoreFxUser5"); GroupData g1 = GroupData.GenerateGroupData("CoreFXGroup5"); DeleteUser(u1.Name); DeleteGroup(g1.Name); try { using (PrincipalContext context = DomainContext) { using (UserPrincipal up = FindUser(u1.Name, context)) { Assert.Null(up); } using (GroupPrincipal gp = FindGroup(g1.Name, context)) { Assert.Null(gp); } using (UserPrincipal user = CreateUser(context, u1)) using (GroupPrincipal group = CreateGroup(context, g1)) { using (UserPrincipal up = FindUser(u1.Name, context)) { Assert.NotNull(up); up.Delete(); } using (GroupPrincipal gp = FindGroup(g1.Name, context)) { Assert.NotNull(gp); gp.Delete(); } } using (UserPrincipal up = FindUser(u1.Name, context)) { Assert.Null(up); } using (GroupPrincipal gp = FindGroup(g1.Name, context)) { Assert.Null(gp); } } } finally { DeleteUser(u1.Name); DeleteGroup(g1.Name); } }
public List <Log> DeleteUsers(List <string> users) { List <Log> result = new List <Log>(); GroupPrincipal group = null; UserPrincipal user = null; Log log = null; try { foreach (string username in users) { foreach (string groupname in GetUserGroupNames(username)) { group = GroupPrincipal.FindByIdentity(context, groupname); group.Delete(); log = new Log(); log.Message = string.Format("Group[{1}] of user[{0}] deleting complete", user.Name, groupname); log.OccurredTime = DateTime.Now; log.OperatorName = GetType().Name; result.Add(log); } user = UserPrincipal.FindByIdentity(context, username); user.Delete(); log = new Log(); log.Message = string.Format("User[{0}] deleting complete", user.Name, username); log.OccurredTime = DateTime.Now; log.OperatorName = GetType().Name; result.Add(log); } } catch (Exception e) { log = new Log(); log.Message = string.Format("User[{0}] deleting fault reason : ", e.Message); log.OccurredTime = DateTime.Now; log.OperatorName = GetType().Name; result.Add(log); } return(result); }
private void btnDeleteGroup_Click(object sender, EventArgs e) { if (lbGroups.SelectedItem == null) { MessageBox.Show("Please select a group"); return; } GroupPrincipal insGroupPrincipal = (GroupPrincipal)lbGroups.SelectedItem; try { insGroupPrincipal.Delete(); insGroupPrincipal.Dispose(); MessageBox.Show("Group deleted."); } catch (Exception ex) { MessageBox.Show(ex.Message); } ListUsers(); }
/// <summary> /// Deletes a group from Active Directory /// </summary> /// <param name="groupname"></param> public void Delete(string groupname) { PrincipalContext pc = null; GroupPrincipal gp = null; try { // Remove all whitespaces groupname = groupname.Replace(" ", string.Empty); this.logger.Debug("Deleting group " + groupname); pc = new PrincipalContext(ContextType.Domain, this.domainController, this.username, this.password); gp = GroupPrincipal.FindByIdentity(pc, IdentityType.Name, groupname); if (gp != null) { gp.Delete(); } } catch (Exception) { throw; } finally { if (gp != null) { gp.Dispose(); } if (pc != null) { pc.Dispose(); } } }
/// <summary> /// Elimina un grupo /// </summary> /// <param name="grupo">Grupo a eliminar</param> public void EliminarGrupo(GroupPrincipal grupo) { grupo.Delete(); }