In the following code,
private bool AuthenticateUser(string userName, string password)
{
try
{
using(var entry = new DirectoryEntry("myLDAP", userName, password))
{
// Attempt to bind to the directory entry
object nativeObject = entry.NativeObject;
return true; // Authentication succeeded
}
}
catch(DirectoryServicesCOMException)
{
// Handle exception for failed authentication
return false;
}
catch(Exception ex)
{
// Handle other exceptions (logging, etc.)
MessageBoxes.msgBoxOK(
"Authentication Error",
$"An error occurred: {ex.Message}",
MessageBoxImage.Error);
return false;
}
}
I am getting a,
'System.DirectoryServices.DirectyEntry.NativeObject.get' times out and needed to be aborted in an unsafe way. This may have corrupted the target process.
But the error does not catch for either the catch(DirectoryServicesCOMException) or the general, catch(Exception ex)
Instead, the code continues in the block and returns true.
Any reason why the try-catch does not catch the timed out error?
object nativeObject = null;then try to assignentry.NativeObjectand finallyreturn nativeObject is not null;ActiveDs.IADsUser nativeObject = (ActiveDs.IADsUser)entry.NativeObject;You'll need to add a reference to the Active DS type library (Interop.ActiveDs.dll).object nativeObject = null; _ = entry.NativeObject; return nativeObject is not null;but that unfortunately has the same behaviour. Thanks for the suggestion though.