c# - UserPrincipal.FindByIdentity 权限

标签 c# .net active-directory ldap directoryservices

我正在尝试使用 .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/

相关文章:

C# 尝试创建带空格的组织单位时出错

python - 是否可以在 python 中返回 AD 中的所有计算机

c# - 如何从嵌套的 try-catch block 中重新抛出先前的异常? (C#)

c# - 如果不能命名互斥锁,它有什么用?

c# - C#中的隐式转换而不创建新对象

c# - UtcNow.ToLocalTime() 在 CentOS 上返回 UTC 时间而不是本地时间

C# 从文本文件中的公钥获取 CngKey 对象

c# - .Net 在编译时将类常量设置为其命名空间

c# - 确定当前用户帐户的密码何时即将过期

c# - 使用 Directory.Getfiles 指定绝对路径