使用 ValidateCredentials(null, null) 测试 PrincipalContext 时出现意外行为

Testing a `PrincipalContext` using `ValidateCredentials(null, null)` behaves unexpectedly

我需要验证用于连接到 AD 服务器的凭据。如果将无效凭据传递给 PrincipalContext(ContextType, String, String, String)PrincipalContext.ConnectedServer 会抛出 System.DirectoryServices.DirectoryServicesCOMException,这是在首次使用 PrincipalContext.

时发现的

我正在尝试使用 PrincipalContext.ValidateCredentials(null, null) 测试凭据,但我遇到了问题。根据 .NET Core 2.0 docs

The ValidateCredentials method binds to the server specified in the constructor. If the username and password parameters are null, the credentials specified in the constructor are validated.

我创建了到服务器的连接。

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
}

文档让我相信这些调用的行为应该相同,但我遇到了不同的结果。为什么会这样?测试连接的正确方法是什么?

我能够通过 运行 您在我计算机上本地帐户下的代码并在构造函数中传递有效的域凭据来复制它。 ValidateCredentials(null, null)确实失败了。

这听起来像是代码或文档中的错误,因此我在 GitHub 上提交了错误:https://github.com/dotnet/corefx/issues/29369

编辑: 看起来他们已决定按原样保留实施并更正文档。