我知道这是一个老问题,但现在可以使用源代码,因此您可以窃取和修改™ 来制作一个接受凭据的版本:
public static bool Exists(string path, string username, string password)
{
DirectoryEntry entry = new DirectoryEntry(path, username, password);
try
{
_ = entry.NativeObject; // throws exceptions (possibly can break applications)
return true;
}
catch (System.Runtime.InteropServices.COMException e)
{
if (e.ErrorCode == unchecked((int)0x80072030) ||
e.ErrorCode == unchecked((int)0x80070003) || // ERROR_DS_NO_SUCH_OBJECT and path not found (not found in strict sense)
e.ErrorCode == unchecked((int)0x800708AC)) // Group name could not be found
return false;
throw;
}
finally
{
entry.Dispose();
}
}
你必须做的一个改变是改变使用Bind
,因为这是一种internal
方法,不能被像我们这样的凡人使用。相反,我只是得到了需要我们NativeObject
的财产。Bind()
你可以像这样使用它:
var ouExists = Exists("LDAP://hadoop.com/OU=Students,DC=hadoop,DC=com", "username", "password");