/// <summary> /// Agrega un grupo dados su nombre y descripción /// </summary> /// <param name="nombre">Nombre del grupo</param> /// <param name="descripción">Descripción del grupo</param> public void AgregarGrupo(string nombre, string descripción) { GroupPrincipal nuevoGrupo = new GroupPrincipal(_dominio); nuevoGrupo.Name = nombre; nuevoGrupo.Description = descripción; nuevoGrupo.Save(); nuevoGrupo.Dispose(); }
public SecurityGroup Create(string parentOU, SecurityGroup group) { PrincipalContext ctx = null; GroupPrincipal grp = null; try { log.DebugFormat("Creating a new group {0}", group.Name); if (string.IsNullOrEmpty(parentOU)) throw new MissingFieldException("Create", "parentOU"); if (string.IsNullOrEmpty(group.Name)) throw new MissingFieldException("SecurityGroup", "Name"); if (string.IsNullOrEmpty(group.SamAccountName)) throw new MissingFieldException("SecurityGroup", "SamAccountName"); if (group.SamAccountName.Length > 19) throw new ArgumentOutOfRangeException(group.SamAccountName); ctx = new PrincipalContext(ContextType.Domain, _domainController, parentOU, _username, _password); grp = new GroupPrincipal(ctx, group.SamAccountName); grp.Name = group.Name; grp.IsSecurityGroup = true; if (!string.IsNullOrEmpty(group.Description)) grp.Description = group.Description; if (!string.IsNullOrEmpty(group.DisplayName)) grp.DisplayName = group.DisplayName; grp.Save(); log.DebugFormat("Successfully created new group {0}", group.Name); // Update the values group.DistinguishedName = grp.DistinguishedName; group.ObjectGUID = (Guid)grp.Guid; return group; } catch (Exception ex) { log.ErrorFormat("Error creating new group {0}. Exception: {1}", group.Name, ex.ToString()); throw; } finally { if (grp != null) grp.Dispose(); if (ctx != null) ctx.Dispose(); } }