【发布时间】:2018-04-27 04:07:56
【问题描述】:
我需要验证用于连接 AD 服务器的凭据。如果将无效凭据传递给PrincipalContext(ContextType, String, String, String),则PrincipalContext.ConnectedServer 会抛出System.DirectoryServices.DirectoryServicesCOMException,这是在首次使用PrincipalContext 时发现的。
我正在尝试使用 PrincipalContext.ValidateCredentials(null, null) 测试凭据,但我遇到了问题。根据.NET Core 2.0 docs
ValidateCredentials 方法绑定到构造函数中指定的服务器。如果用户名和密码参数为空,则验证构造函数中指定的凭据。
我创建了一个到服务器的连接。
string username = "username"
string password = "password"
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "my_domain.local", username, password);
然后我尝试测试连接:
if (ctx.ValidateCredentials(null, null))
{
// This block does not get hit!
// This is surprising because the credentials are valid
}
与以下行为不同:
if (ctx.ValidateCredentials(username, password))
{
// Credentials are valid, this block gets hit
}
文档让我相信这些调用的行为应该相同,但我遇到了不同的结果。为什么会这样?测试连接的正确方法是什么?
【问题讨论】:
标签: c# active-directory .net-core principalcontext