0

I'm trying to create a user and add it to 3 groups, the creation is working properly but adding user to groups sometimes it's working and sometimes show this error:

there is no such object on the server

my code for adding user to group :

try
            {
                DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + group, ADUsername, ADPassword);
                if (dirEntry != null)
                {
                    dirEntry.Properties["member"].Add(userPrincipal.DistinguishedName);
                    dirEntry.CommitChanges();
                    dirEntry.Close();
                }
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //doSomething with E.Message.ToString();

            }
DevBassem
  • 13
  • 1
  • 2
  • 7
  • Read this : [c# - Adding and removing users from Active Directory groups in .NET - Stack Overflow\[^\]](https://stackoverflow.com/questions/2143052/adding-and-removing-users-from-active-directory-groups-in-net) – DevBassem Sep 18 '18 at 09:18

1 Answers1

1

I have experienced this and in my situation it was because the newly created object hadn’t replicated to all of our DCs. I resolved this by introducing a 3 second delay after I created the user. I then proceeded with adding the user to the groups.

Jono
  • 49
  • 1
  • 8
  • Yes, this has been my first thought too after reading this question. Some developers don't like sleeps (sometimes for good reason), but real life requires this in some situations :-) Perhaps only 1 sec delay and using a retry loop. – Rainer Schaack Apr 18 '19 at 19:19