系统.UnauthorizedAccessException调用UserPrincipal.SetPassword
本文关键字:SetPassword UserPrincipal 调用 UnauthorizedAccessException 系统 | 更新日期: 2023-09-27 18:09:41
当我运行这段代码
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
adHost,
adRoot,
ContextOptions.SimpleBind,
adUsername,
adPassword);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username);
user.SetPassword(password);
user.Save();
我得到这个异常
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.UnauthorizedAccessException: One or more input parameters are invalid
代码从命令行运行,使用"runas/user:(domainadminuser也是本地管理员)使用相同的凭据(domainadminuser)创建上下文。我已经检查了所有的用户名,密码等是正确填写这与我创建PrincipalContext的方式有关吗?
我完全卡住了。有人有什么想法吗?
感谢(更新)这是我用来让它工作的代码。我想可能是validatecredals(可能)让它活了起来
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, parameters["adHost"] );
ctx.ValidateCredentials(parameters["adUsername"], parameters["adPassword"], ContextOptions.SimpleBind);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username);
user.SetPassword(password);
user.Save();
下面的代码可以很好地用于我们内部开发的密码请求管理系统,请尝试让我知道:
PrincipalContext context = new PrincipalContext( ContextType.Domain, null, adAdminLogin, adAdminPassword );
UserPrincipal user = UserPrincipal.FindByIdentity( context, adUserLogin );
user.SetPassword( adUserNewPassword );
就Active-Directory与标准LDAP协议有关而言,没有SSL的简单绑定不允许更改任何密码。显然,这里使用的是可以使用非标准协议与服务器通信的类,但是SimpleBind上下文选项可以切换到标准LDAP。看看@CodeCanvas代码
创建Context时,确保将ContextOptions
设置为ContextOptions.Negotiate
。如果您提到了ContextOptions.SimpleBind
, SetPassword
可能不起作用。
PrincipalContext oPrincipalContext =
new PrincipalContext (ContextType.Domain, "Name", "DefaultOU(if required)",
ContextOptions.Negotiate, "Service Account(if required)",
"Service password");