使用`ValidateCredentials(null,null)`测试`PrincipalContext`的行为异常 [英] Testing a `PrincipalContext` using `ValidateCredentials(null, null)` behaves unexpectedly

查看:181
本文介绍了使用`ValidateCredentials(null,null)`测试`PrincipalContext`的行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to validate the credentials that are used to connect to an AD server. If if pass invalid credentials to PrincipalContext(ContextType, String, String, String), PrincipalContext.ConnectedServer throws a System.DirectoryServices.DirectoryServicesCOMException which is discovered on the first use of the PrincipalContext.

我正在尝试使用 PrincipalContext.ValidateCredentials(null,null)测试凭据,但是出现问题。根据。 NET Core 2.0文档

I am trying to test the credentials with PrincipalContext.ValidateCredentials(null, null) but I am having issues. According to the .NET Core 2.0 docs


ValidateCredentials方法绑定到构造函数中指定的服务器。如果用户名和密码参数为null,则将验证在构造函数中指定的凭据。

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.

我创建了与服务器的连接。 / p>

I create a connnection to the server.

string username = "username"
string password = "password"

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "my_domain.local", username, password);

然后要测试连接,请尝试:

And then to test the connection I try:

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
}

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

The docs lead me to believe these calls should behave identically yet I am experiencing different results. Why is this and what is the proper way to test a connection?

推荐答案

我能够通过在我计算机上的本地帐户,并在构造函数中传递有效的域凭据。 ValidateCredentials(null,null)确实失败。

I was able to replicate this by running your code under a local account on my computer and passing valid domain credentials in the constructor. ValidateCredentials(null, null) does indeed fail.

这听起来像是一个错误,无论是在代码中还是在文档,所以我在GitHub上提交了一个错误: https://github.com/dotnet / corefx / issues / 29369

This sounds like a bug, either in the code or in the documentation, so I've filed a bug on GitHub: https://github.com/dotnet/corefx/issues/29369

编辑:看起来他们决定将实现保持原样并更正文档。

looks like they've decided to leave the implementation as-is and correct the documentation.

这篇关于使用`ValidateCredentials(null,null)`测试`PrincipalContext`的行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆