LDAP在Linux下使用dotnet core

12
我正在开发一个基于.net core(2.2.103)的应用程序,该程序必须连接到LDAP服务器。在我的运行Windows的开发计算机上,我使用System.DirectoryServices命名空间来完成这个任务。 然而,该应用程序将在Linux(Ubuntu)上运行,并且我遇到了PlatformNotSupportedException异常,于是我添加了对<PackageReference Include="Novell.Directory.Ldap" Version="2.2.1" />的引用,并使用它来连接LDAP服务器。
不幸的是,当连接被释放时,这会抛出另一个PlatformNotSupportedException异常(但原因是线程中止)。
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()

有可靠的适用于Linux的dotnet core LDAP实现吗?

1
检查异常信息:Thread abort is not supported。看起来您忘记释放连接对象了。Finalize()方法由垃圾回收器调用以清理孤立的对象。 - Panagiotis Kanavos
1
无论如何,.NET Standard包是Novell.Directory.Ldap.NETStandard,而不是Novell.Directory.Ldap。 - Panagiotis Kanavos
4个回答

20
你尝试使用的软件包最后更新于2014年,不符合.NET Core或.NET Standard标准。你可以尝试使用Novell.Directory.Ldap.NETStandard。尽管名称中含有Novell,但这不是一个Novell库。在NuGet中有其他LDAP库,但这似乎是最常用且仍在积极开发中的库。异常提示你可能忘记了对连接进行处理。 Finalize仅由垃圾收集器调用。 此答案展示了如何使用Novell.Directory.Ldap.NETStandard来验证用户:
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;
}

连接是在 using 块内创建的,这确保它在执行离开块的作用域时被处理掉。


1
旧包的LdapConnection没有实现IDisposable,但是感谢您指出更新的包!我能够使用我的所有代码而无需更改任何内容(除了添加using语句)。 - Philipp
仅供2022年阅读的人参考 - 我也在寻找解决方案,我相信Ldap4Net现在支持.NET Standard - 最低要求是2.0 - 兼容的.NET运行时:.NET Core,Mono,.NET Framework。 - PKCS12

11
.NET 5发布后,微软增加了跨平台支持(Windows、Linux和macOS)的库System.DirectoryServices.Protocols,它是建立在低级LDAP API上的System.DirectoryServices。希望将来也能跨平台支持System.DirectoryServices

来源:.NET5 - 将directoryservices.protocols扩展到Linux和macOS

我个人仍然使用Novell.Directory.Ldap.NETStandard,但我对它不太满意。希望我能抽出时间,很快切换到system.directoryservices.protocols或者更好的system.directoryservices库。


1
System.DirectoryServices.Protocols 的新跨平台支持在 Windows 上运行良好,但在 Linux 上存在一些问题。例如,SSL/TSL 无法正常工作。请参见此处:https://github.com/dotnet/runtime/issues/43890 和 https://github.com/dotnet/runtime/issues/36947。 - Ulf M.
似乎36947 - wldap32.dll和libldap之间的兼容性仍未解决。这个评论包含了在Windows和Linux下应该能工作的代码。 据我所知,PR 52904解决了43890 - 在Linux上为LdapSessionOptions添加SSL支持 - surfmuggle

3

1
如何在Linux上使用此软件包。项目页面建议在Linux上安装OpenLDAP客户端库,但我在OpenLDAP项目中找不到任何相关步骤。 - VinayC
1
您可以安装ldap-utils。对于Debian系统,请执行以下命令:sudo apt install ldap-utils - Alexander Chermyanin
还在使用这个吗? - kabuto178

0
在我的情况下,Novell.Directory.Ldap.NETStandard 在 Windows 上运行良好,但在运行在 Linux VM 上的 Docker 容器上却无法正常工作。
在 Linux 上,问题出在 DNS 设置上。将 Linux VM 的 DNS 设置与 LDAP/Active Directory 服务器设置相同,并重新启动 VM 后,容器就可以正常工作了。希望这能对某些人有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接