调用UserPrincipal.SetPassword的System.UnauthorizedAccessException [英] System.UnauthorizedAccessException calling UserPrincipal.SetPassword

查看:85
本文介绍了调用UserPrincipal.SetPassword的System.UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时

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的方式有关吗?

The code is running from a command line using "runas /user: (domainadminuser is also a local admin) The context is created using the same credentials (domainadminuser) I've checked that all usernames, passwords etc are populated correctly Is it something to do with the way I am creating the PrincipalContext?

我完全被困住了。有人有什么想法吗?

I'm completely stuck. Does anyone have any ideas?

谢谢

[更新]
这是我用来使其正常工作的代码。也许ValidateCredentials是使它生效的东西(可能)

[UPDATE] Here's the code I used to get it working. I think maybe the ValidateCredentials was the thing that kicked it into life (possibly)

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();


推荐答案

下面是适合密码请求的代码我们内部开发的管理系统,请尝试让我知道:

Below is the code that works fine for a password request management system we developed in-house, do try and let me know:

PrincipalContext context = new PrincipalContext( ContextType.Domain, null, adAdminLogin, adAdminPassword );
UserPrincipal user = UserPrincipal.FindByIdentity( context, adUserLogin );
user.SetPassword( adUserNewPassword );

这篇关于调用UserPrincipal.SetPassword的System.UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆