ADLib.ActiveDirectory.RemoveUserFromGroup C# (CSharp) Method

RemoveUserFromGroup() public method

Removes the user from the specified group
public RemoveUserFromGroup ( System.DirectoryServices.AccountManagement.UserPrincipal adUser, string groupName ) : void
adUser System.DirectoryServices.AccountManagement.UserPrincipal User to remove
groupName string Group from which the user should be removed
return void
        public void RemoveUserFromGroup(UserPrincipal adUser, string groupName)
        {
            GroupPrincipal g = GroupPrincipal.FindByIdentity(_GlobalContext, groupName);
            g.Members.Remove(adUser);
            g.Save();
        }

Usage Example

 /// <summary>
 /// Remove the ADUser from a group
 /// </summary>
 /// <param name="groupName">The name of the group from which the ADUser should be removed</param>
 public void removeFromGroup(string groupName)
 {
     try
     {
         _connection.RemoveUserFromGroup(_sourceUser, groupName);
     }
     catch (Exception ex)
     {
         string msg = String.Format("Error removing user '{0}' from group '{1}': '{2}'", this.DisplayName, groupName, ex.Message);
         Console.WriteLine(msg);
         throw new ADException(msg, ex);
     }
 }