我们在使用活动目录和移动/重命名OU时遇到问题。这只发生在我们在两个域控制器之间复制时。我们得到的例外是:
System.ServiceModel.FaultException:服务器上没有这样的对象。 (HRESULT异常:0x80072030)
当我们尝试在活动目录中移动和重命名OU时,我们会收到此错误消息的变体。这是有问题的代码:
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();
}
}
}
所以我有两个问题:
答案 0 :(得分:0)
您可能应该在尝试移动之前将更改提交给重命名。
entry.Rename(string.Format("CN={0}", newEmail));
entry.CommitChanges(); // add this line