更新 UserPrincipal 上的名称字段
当我尝试更新 UserPrincipal(实际上是主体)上的名称字段(对应于 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 对象的安全权限。如果我更新任何其他字段(例如姓氏、GivenName),一切都会正常。
编辑:
我已经能够完成我需要做的事情(使用 ADSI),但我必须在模拟下运行以下代码。模拟代码很丑陋,下面的代码脱离了我更新 AD 数据的其他方式(使用 DirectoryServices.AccountManagement),所以我想获得更好的解决方案。
using (var companyOU = new DirectoryEntry("LDAP://" + company.UserAccountOU)) {
companyOU.Invoke("MoveHere", "LDAP://" + user.DistinguishedName, "cn=Name\, Test");
}
When I try to update the Name field (corresponds to the CN) on UserPrincipal (Principal, really), I get an error "The server is unwilling to process the request" on the call to UserPrincipal.Save().
I've checked to make sure there isn't another object in the same OU with the same Name (CN).
The PrincipalContext I'm operating at is the domain root (not exactly at the OU level where the user account exists).
What reason might there be for this error? Is it something that might be security policy related (even though I'm able to update all the other fields)?
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();
}
The user I am using to create the PrincipalContext has the security rights to modify AD objects. If I update any other of the other fields (e.g. Surname, GivenName), everything works fine.
EDIT:
I've been able to accomplish what I need to do (using ADSI), but I have to run the following code under impersonation. The impersonation code is ugly, and the code below breaks away from the other way I'm updating AD data (using DirectoryServices.AccountManagement), so I'd like to get a better solution.
using (var companyOU = new DirectoryEntry("LDAP://" + company.UserAccountOU)) {
companyOU.Invoke("MoveHere", "LDAP://" + user.DistinguishedName, "cn=Name\, Test");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种更清洁的方式
This is a cleaner way
我发现做到这一点的唯一方法是在我的问题的编辑部分。基本上,您不能使用 UserPrincipal 类。 CN 属性有一些特殊之处,您需要下拉一个级别并使用 DirectoryEntry(LDAP 字符串),并调用“MoveHere”ADSI 命令来重命名用户帐户。
The only way I've found to do this is in the EDIT section in my question. Basically, you cannot use the UserPrincipal class. There is something special about the CN attribute, and you need to drop down a level and use DirectoryEntry, an LDAP string, and invoke the "MoveHere" ADSI command to rename the user account.