c# - 针对 AD 异常验证用户名和密码

标签 c# active-directory

我有以下代码来验证 Active Directory 用户(Win7 上的 VS2008)。

try
{
    PrincipalContext pc = new PrincipalContext(ContextType.Domain, strDomainName))
    isPass = pc.ValidateCredentials(strDomainUserName, strDomainUserPass);
}
catch (Exception ex)
{
    Logger.Log("authentiate domain user fail: {0},{1}", ex.ToString(), strDomainUserLoginName);
}

对于某些 AD 用户,没问题,但对于 AD 用户,我会得到以下异常: System.DirectoryServices.Protocols.DirectoryOperationException: 服务器无法处理目录请求。

System.DirectoryServices.Protocols.ErrorChecking.CheckAndSetLdapError(Int32 error)

System.DirectoryServices.Protocols.LdapSessionOptions.FastConcurrentBind()

System.DirectoryServices.AccountManagement.CredentialValidator.BindLdap(NetworkCredential creds, ContextOptions contextOptions)

System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password)

System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password)

那么Active Directory域用户设置会不会导致这样的异常呢?

最佳答案

对不起,我上面的描述是错误的。事实上,所有的 AD 用户验证都会得到这样的异常。

而工作环境是: (1) 如果验证码运行在恰好是AD机器的机器上,那么代码就会出现这样的异常。 (2)否则,不存在该异常。

为 ValidateCredentials 附加 ContextOptions.Negotiate 参数以解决问题。

关于c# - 针对 AD 异常验证用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14975797/

相关文章:

c# - 在 WPF 图像控件上设置背景图像?

c# - SQL 查询返回列表

powershell - 如何在 PowerShell 中获取以 "ABC_"开头的 Active Directory 组名称列表?

c# - 使用 C# 获取 Active Directory 中用户的父 OU

c# - 无法获取 Active Directory 中域用户中存在的用户?

c# - ScrollViewer 上的 DataGrids 阻止它滚动

c# - 从 AssemblyInfo.cs 获取程序集版本

c# - 直接P2P连接

c# - 使用 c# 和 TLSv1.2 连接到 LDAP

azure - 如何从订阅中删除 Azure Active Directory