docker - Linux 托管和 .NET Core Docker:PlatformNotSupportedException:此平台不支持 System.DirectoryServices.AccountManagement
问题描述
我在 .NET Core 2.0 中实现了 Web api,并实现了基本身份验证和 Active Directory。我们正在使用 docker 进行部署。以下我为 Active Directory 身份验证编写的代码:
using (PrincipalContext pContext = new PrincipalContext(ContextType.Domain, "xyz"))
{
try
{
// validate the credentials
bool isValid = pContext.ValidateCredentials(userName, password, ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing);
if (!isValid)
{
return false;
}
}
catch(Exception)
{
return false;
}
return true;
}
要使用 PrincipalContext,我添加了
System.DirectoryServices.AccountManagement;
这完全适用于 Windows 机器。使用 Docker 容器也可以在 Windows 环境中正常工作。但是我们的生产服务器是基于 Linux 的,它不能在 Linux 托管系统上运行。它显示错误,例如
PlatformNotSupportedException:此平台不支持 System.DirectoryServices.AccountManagement。
可以做些什么来使它工作?
谢谢!
解决方案
当我们使用 docker 容器来托管我们的应用程序时,不支持命名空间(System.DirectoryServices.AccountManagement)。但是我们可以使用这个 nuget 包https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard及其与 docker 容器托管一起使用类似的设置来验证 Active Directory 上的用户凭据。
// using Novell.Directory.Ldap;
try
{
using (var connection = new LdapConnection())
{
connection.Connect("DomainUrl", 389);
connection.Bind("username@domain", "password");
if (connection.Bound)
{
return true;
}
else
{
return false;
}
}
}
catch (LdapException ex)
{
return false;
}
推荐阅读
- java - maven - 参考本地设置 .xml
- amazon-web-services - 具有数据敏感对象名称的 AWS S3 对象
- powershell - 带空格的powershell命令参数
- python - Pyinstaller 和 PyCryptoDome,不兼容?或者其他的东西?
- node.js - 使用 node/vuejs,如何在客户端发出 google place API 请求
- typo3 - Typo3:TCA 表中没有列定义
- django - django 获取最后插入的不同行
- javascript - 如何避免 OVER_QUERY_LIMIT nodejs
- vue.js - Vue 中的模块样式和作用域样式有什么区别?
- sql - 从动态字符串返回列名