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