gpt4 book ai didi

c# - 如何为 DirectoryEntry.Exists 提供凭据?

转载 作者:太空狗 更新时间:2023-10-29 17:37:21 25 4
gpt4 key购买 nike

今天早上我发现了一个不错的方法 ( DirectoryEntry.Exists ),它应该能够检查服务器上是否存在 Active Directory 对象。所以我尝试了一个简单的:

if (DirectoryEntry.Exists(path)) {}

当然,它没有任何重载来提供凭据。因为,如果未提供凭据,我会收到此异常:

Logon failure: unknown user name or bad password. (System.DirectoryServices.DirectoryServicesCOMException)

是否有任何其他选项可以让我在 AD 服务器上验证我的代码?或者检查对象是否存在?

最佳答案

在这种情况下,您不能像您所说的那样使用静态方法 Exists :

DirectoryEntry directoryEntry = new DirectoryEntry(path);
directoryEntry.Username = "username";
directoryEntry.Password = "password";

bool exists = false;
// Validate with Guid
try
{
var tmp = directoryEntry.Guid;
exists = true;
}
catch (COMException)
{
exists = false;
}

关于c# - 如何为 DirectoryEntry.Exists 提供凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4284253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com