繁体   English   中英

LDAP 与 do.net 核心在 Linux

[英]LDAP with dotnet core under Linux

我正在开发一个基于 .net 核心 (2.2.103) 的应用程序,它必须连接到 LDAP 服务器。 在运行 Windows 的开发机器上,我使用System.DirectoryServices命名空间来执行此操作。 但是,该应用程序必须在 Linux (Ubuntu) 上运行并且我得到了PlatformNotSupportedException ,因此我添加了对<PackageReference Include="Novell.Directory.Ldap" Version="2.2.1" />的引用并使用了它。

不幸的是,这会在处理连接时抛出另一个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 上是否有可靠的 LDAP do.net 核心实现?

您尝试使用的包上次更新是在 2014 年。它既不符合 .NET Core 也不符合 .NET Standard。

您可以尝试使用Novell.Directory.Ldap.NETStandard 尽管名称如此,但这不是 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块内创建的,以确保一旦执行离开块的范围,它就会被处理掉

随着.NET 5 的发布,微软为库System.DirectoryServices.Protocols添加了跨平台支持(windows、linux、macos)。 System.DirectoryServices是基于它的低级 LDAP API。 我希望他们能让System.DirectoryServices在未来也跨平台。

来源: .NET 5 - 将 directoryservices.protocols 扩展到 linux 和 macos

我个人仍然使用Novell.Directory.Ldap.NETStandard ,但我对此并不满意。 我希望我能找到一些时间并尽快切换到system.directoryservices.protocols甚至更好的system.directoryservices库。

如果您想使用跨平台解决方案,您可以使用库https://github.com/flamencist/ldap4net 该库支持集成身份验证,如 Kerberos\\gssapi\\negotiate

在我的例子中,Novell.Directory.Ldap.NETStandard 在 Windows 上运行良好,但在 Linux VM 上运行的 Docker 容器上运行良好。

在 Linux 中,问题是 DNS 设置。 将 Linux VM 的 DNS 设置与 LDAP/Active Directory 服务器设置相同并重新启动 VM 后,容器工作正常。 希望它可以帮助某人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM