8

当我尝试更新 UserPrincipal(实际上是Principal)上的 Name 字段(对应于 CN)时,在调用 UserPrincipal.Save() 时出现错误“服务器不愿意处理请求”。

我已经检查以确保在同一个 OU 中没有另一个具有相同名称 (CN) 的对象。

我正在操作的 PrincipalContext 是域根目录(不完全位于用户帐户所在的 OU 级别)。

这个错误可能有什么原因?是否可能与安全策略相关(即使我能够更新所有其他字段)?

using (var context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["domain"], ConfigurationManager.AppSettings["rootDN"], ContextOptions.Negotiate, ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"])) {
    var user = UserPrincipal.FindByIdentity(context, IdentityType.Sid, "..."); // SID abbreviated

    user.Name = "Name, Test";

    user.Save();
}

我用来创建 PrincipalContext 的用户拥有修改 AD 对象的安全权限。如果我更新任何其他字段(例如姓氏、名字),一切正常。

编辑:

我已经能够完成我需要做的事情(使用 ADSI),但我必须在模拟下运行以下代码。模拟代码很丑陋,下面的代码脱离了我更新 AD 数据的另一种方式(使用 DirectoryServices.AccountManagement),所以我想得到一个更好的解决方案。

using (var companyOU = new DirectoryEntry("LDAP://" + company.UserAccountOU)) {
    companyOU.Invoke("MoveHere", "LDAP://" + user.DistinguishedName, "cn=Name\, Test");
}
4

2 回答 2

17

这是一种更清洁的方式

using (var context = new PrincipalContext(ContextType.Domain))
{
    var group = GroupPrincipal.FindByIdentity(context, groupName);
    group.SamAccountName = newGroupName;
    group.DisplayName = newGroupName;
    group.Save();

    var dirEntry = (DirectoryEntry)group.GetUnderlyingObject();    
    dirEntry.Rename("CN=" + newGroupName);
    dirEntry.CommitChanges();
}
于 2014-01-09T12:19:47.127 回答
3

我发现这样做的唯一方法是在我的问题的编辑部分。基本上,您不能使用 UserPrincipal 类。CN 属性有一些特殊之处,您需要下拉一个级别并使用 DirectoryEntry(一个 LDAP 字符串)并调用“MoveHere”ADSI 命令来重命名用户帐户。

于 2011-09-29T18:49:09.650 回答