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

查看:30
本文介绍了使用 `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 方法绑定到构造函数中指定的服务器.如果用户名和密码参数为空,则验证构造函数中指定的凭据.

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.

我创建了一个到服务器的连接.

I create a connnection to the server.

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
}

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

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天全站免登陆