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

标签 c# .net active-directory directoryentry

今天早上我发现了一个不错的方法 ( 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/

相关文章:

delphi - 使用 Unicode 的 AD 身份验证

c# - 具有 "Refresh"条目的组合框

c# - Asp.net Core 迁移,后操作不再有效

c# - USING 语句的返回值

.net - Silverlight 5 列表框大小调整

c# - 这些枚举代码的功能相同吗?

docker - 主机是否可以通过Docker容器上运行的KDC进行身份验证?

arrays - PowerShell-使用不同的键创建哈希表数组

c# - 将 WPF (XAML) 控件转换为 XPS 文档

c# - 如何从 observable.Subscribe 函数中获取字符串列表