I am developing an application based on .net core (2.2.103) which must connect to an LDAP server. On my development machine running Windows, I used the System.DirectoryServices
namespace to do so.
However, the application will have to run on Linux (Ubuntu) and I got a PlatformNotSupportedException
, so I added a reference to
<PackageReference Include="Novell.Directory.Ldap" Version="2.2.1" />
and used that.
Unfortunately, this throws another PlatformNotSupportedException
(but because of Thread abort) when the Connection is disposed:
Unhandled Exception: System.PlatformNotSupportedException: Thread abort is not supported on this platform.
at System.Threading.Thread.Abort()
at Novell.Directory.Ldap.Connection.Dispose(Boolean disposing, String reason, Int32 semaphoreId, InterThreadException notifyUser)
at Novell.Directory.Ldap.Connection.destroyClone(Boolean apiCall)
at Novell.Directory.Ldap.LdapConnection.Finalize()
Is there any reliable LDAP implementation for dotnet core on Linux?
The . NET Core runtime allows you to run applications on Linux that were made with . NET Core but didn't include the runtime. With the SDK you can run but also develop and build .
LDAP, or Lightweight Directory Access Protocol, is an open-vendor protocol for accessing directory information, such as X. 500 service containers. It is useable in Linux and other OS environments. This write-up describes setting up the LDAP directory on a Linux server.
LDAP. We have an web application developed using c#(VS 2008/3.5 framework). The application uses the mode of authentication as "Windows" with a service account present in domain (Domain1) to run the application as ASP.Net user. We have authentication to be done for the users present in different domain (Domain 2).
The package you tried to use was last updated in 2014. It's neither .NET Core nor .NET Standard compliant.
You can try Novell.Directory.Ldap.NETStandard instead. Despite the name, this isn't a Novell library. There are other LDAP libraries in NuGet but this seems to be the most popular and is still actively developed.
The exception suggests you forgot to dispose the connection too. Finalize
is only called by the garbage collector.
This answer shows how to use Novell.Directory.Ldap.NETStandard to authenticate a user :
public bool ValidateUser(string domainName, string username, string password)
{
string userDn = $"{username}@{domainName}";
try
{
using (var connection = new LdapConnection {SecureSocketLayer = false})
{
connection.Connect(domainName, LdapConnection.DEFAULT_PORT);
connection.Bind(userDn, password);
if (connection.Bound)
return true;
}
}
catch (LdapException ex)
{
// Log exception
}
return false;
}
The connection is created inside a using
block which ensures it gets disposed as soon as execution leaves the block's scope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With