Replication issues with Active Directory and multiple DCs

1

We are having issues using active directory and moving/renaming OUs. This only occurs when we replicate between two domain controllers. The exception we get is:

System.ServiceModel.FaultException: There is no such object on the server. (Exception from HRESULT: 0x80072030)

We get variations of this error message when we try to move and rename an OU in active directory. Here's the code in question:

PrincipalContext context = GetPrincipalContext();

using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, IdentityType.Guid, id.ToString()))
{
    if (principal == null)
    {
        throw new InvalidOperationException();
    }

    string oldEmail = principal.EmailAddress;

    principal.EmailAddress = newEmail;
    principal.Save();

    DirectoryEntry entry = principal.GetUnderlyingObject() as DirectoryEntry;
    DirectoryEntry targetDirectoryEntry = null;
    string target = null;

    // Access the underlying DirectoryEntry to rename it:
    try
    {
        if (entry == null)
        {
            throw new InvalidOperationException();
        }

        entry.RefreshCache();
        entry.Rename(string.Format("CN={0}", newEmail));

        // Move the DirectoryEntry to the correct location.
        target = BuildOrganizationalUnitName(newEmail);

        targetDirectoryEntry = FindDirectoryEntry(target);
        if (targetDirectoryEntry == null)
        {
            throw new InvalidOperationException();
        }
        entry.MoveTo(targetDirectoryEntry);
        entry.CommitChanges();
    }
    catch (Exception e)
    {
        // do some logging
    }
    finally
    {
        if (entry != null)
        {
            entry.Dispose();
        }

        if (targetDirectoryEntry != null)
        {
            targetDirectoryEntry.Dispose();
        }
    }
}

So I have two questions:

  1. Is there anything wrong with the above code, which is attempting to move and rename an OU?
  2. If not, is there any way to ensure that the two DCs remain in sync after a move/rename?
c#
active-directory
ldap
replication
asked on Stack Overflow Jun 7, 2012 by user1185361

1 Answer

0

You probably should commit the changes to the rename before trying to move it.

entry.Rename(string.Format("CN={0}", newEmail));
entry.CommitChanges();  // add this line
answered on Stack Overflow Nov 26, 2012 by Sean Hall

User contributions licensed under CC BY-SA 3.0