[英]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.