UnauthorizedAccessException:更改AD密码时拒绝访问 [英] UnauthorizedAccessException: Access is denied when changing AD password
问题描述
我尝试在Server 2012中编写用于更新域用户密码的C#代码。我根据此堆栈溢出答案
I am try to write C# code for update Domain User Password in Server 2012. I'm using following code according to this Stack Overflow answer
using (var context = new PrincipalContext(ContextType.Domain, "test.com"))
{
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
{
user.SetPassword(newPassword);
//user.ChangePassword(oldPassword, newPassword);
user.Save();
}
}
在运行代码时出现以下异常
in getting following exception when running code
System.Reflection.TargetInvocationException:调用的目标抛出了
异常。 --->
System.UnauthorizedAccessException:访问被拒绝。 (来自
HRESULT的异常:0x80070005(E_ACCESSDENIED))
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
我还禁用了密码策略。有什么建议吗?
I also disabled password policy. Any advice?
推荐答案
您的代码所运行的帐户没有权限。您有两种选择:
The account your code is running under doesn't have permissions. You have two options:
- 使用具有正确权限的凭据运行程序,或者
- 使用
PrincipalContext
的不同构造函数,并传递具有设置密码权限的用户名和密码:
var context = new PrincipalContext(ContextType.Domain, "test.com", "domain\username", "password");
这篇关于UnauthorizedAccessException:更改AD密码时拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!