如何提供DirectoryEntry.Exists凭据?凭据、DirectoryEntry、Exists

由网友(我疯我傻我等你)分享简介:今天早上,我发现了一个不错的方法(的DirectoryEntry。存在),这应该是能够检查的Active Directory对象是否存在于服务器上。于是,我就用一个简单的:This morning I discovered a nice method (DirectoryEntry.Exists), that sho...

今天早上,我发现了一个不错的方法(的DirectoryEntry。存在),这应该是能够检查的Active Directory对象是否存在于服务器上。于是,我就用一个简单的:

This morning I discovered a nice method (DirectoryEntry.Exists), that should be able to check whether an Active Directory object exists on the server. So I tried with a simple:

if (DirectoryEntry.Exists(path)) {}

当然,它没有任何的过载提供凭据它。因为,如果不提供凭据,我得到这个异​​常:

Of course it lacks any overloads to provide credentials with it. Because, if credentials are not provided I get this Exception:

登录失败:未知的用户名或   密码错误。   (System.DirectoryServices.DirectoryServicesCOMException)

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

是否有任何其他选项,让我在AD服务器来验证我的code的可能性?或者检查对象的存在?

Is there any other option that gives me the possibility to authenticate my code at the AD server? Or to check the existence of an object?

推荐答案

在这种情况下,你不能用静态的方法,你说的存在:

In this case you can't use the static method Exists as you said :

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; 
}
阅读全文

相关推荐

最新文章