首页 > 解决方案 > 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。

可以做些什么来使它工作?

谢谢!

标签: dockeractive-directorydockerfileasp.net-core-2.0basic-authentication

解决方案


当我们使用 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;
}

推荐阅读