我正在尝试使用 .NET System.DirectoryServices.AccountManagement
库来获取特定 Active Directory 用户的 UserPrincipal。
我有以下代码:
PrincipalContext context = new PrincipalContext(ContextType.Domain, "DomainName");
userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
此代码以有效的域用户身份运行,但是当我执行它时出现以下异常:
System.DirectoryServices.DirectoryServicesCOMException (0x8007052E): Logon failure: unknown user name or bad password.
有趣的是,我可以毫无问题地使用相同的上下文进行以下调用:
context.ValidateCredentials(username, password, ContextOptions.Negotiate)
想法?
最佳答案
您需要使用带有用户名和密码的 PrincipalContext
构造函数。
Validate 起作用的原因是因为它使用提供的凭据绑定(bind)到目录。
关于c# - UserPrincipal.FindByIdentity 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3579588/