1

Closed

Bug - Adding users to a group

description

I found that when you add a user to an existing group that has other users. All of the other users get deleted and the one you are adding gets added to the group. Here is the code.
        using (var groupObject = GroupObject.FindOneByCN(adOperator, "TestDemoGroup"))
        {
            groupObject.Members = new List<string> {"CN=John Doe,OU=Technology,DC=acme,DC=local"};
            groupObject.Save();
        } 
Solution: Need to delete or comment out the line that clears the properties. See ADOject.cs
    /// <summary>
    /// Set the attribute value.
    /// </summary>
    /// <typeparam name="TAttributeValue">The attribute value generic type.</typeparam>
    /// <param name="attributeName">The attribute name.</param>
    /// <param name="attributeValue">The attribute value.</param>
    public void SetAttributeValue<TAttributeValue>(string attributeName, TAttributeValue attributeValue)
    {
        if (typeof(TAttributeValue) == typeof(IList<string>) || typeof(TAttributeValue) == typeof(List<string>))
        {
            var attributeValueItems = attributeValue as IList<string>;
            if (attributeValueItems != null)
            {
                // ***This line is the problem so we comment it out***
                //this.DirectoryEntry.Properties[attributeName].Clear();
                foreach (var attributeValueItem in attributeValueItems)
                {
                    this.DirectoryEntry.Properties[attributeName].Add(attributeValueItem);
                }
            }
        }
        else
        {
            this.DirectoryEntry.Properties[attributeName].Value = attributeValue;
        }
    }

Thanks.
Closed Dec 13, 2014 at 7:06 AM by pangxiaoliang
Actually, I think that you should update the code as following:using (var groupObject = GroupObject.FindOneByCN(adOperator, "TestDemoGroup"))


{
    if(groupObject.Members!=null && groupObject.Members.Length!=0)
    {
           groupObject.Members.Add("CN=John Doe,OU=Technology,DC=acme,DC=local"); // Do not use a new List instance. 
    }
    else
    {
           groupObject.Members = new List {"CN=John Doe,OU=Technology,DC=acme,DC=local"};
    }
    groupObject.Save();
}

comments

pangxiaoliang wrote Oct 16, 2014 at 3:16 PM

Actually, I think that you should update the code as following:
using (var groupObject = GroupObject.FindOneByCN(adOperator, "TestDemoGroup"))
    {
        if(groupObject.Members!=null && groupObject.Members.Length!=0)
        {
               groupObject.Members.Add("CN=John Doe,OU=Technology,DC=acme,DC=local"); // Do not use a new List instance. 
        }
        else
        {
               groupObject.Members = new List<string> {"CN=John Doe,OU=Technology,DC=acme,DC=local"};
        }
        groupObject.Save();
    } 

wrote Nov 6, 2014 at 7:17 AM

Actually, I think that you should update the code as following:using (var groupObject = GroupObject.FindOneByCN(adOperator, "TestDemoGroup"))


{
        if(groupObject.Members!=null && groupObject.Members.Length!=0)
        {
               groupObject.Members.Add("CN=John Doe,OU=Technology,DC=acme,DC=local"); // Do not use a new List instance.
        }
        else
        {
               groupObject.Members = new List<string> {"CN=John Doe,OU=Technology,DC=acme,DC=local"};
        }
        groupObject.Save();
    }

wrote Nov 6, 2014 at 7:20 AM

wrote Dec 13, 2014 at 7:06 AM