当我尝试更新 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");
}