【问题标题】:How to use encrypted password in source code for Directory Services authentication如何在源代码中使用加密密码进行目录服务身份验证
【发布时间】:2015-11-24 15:03:33
【问题描述】:

我编写了一个程序,通过 PrincipalContext 读取我们 Active Directory 中用户的 UserPrincipal。为此,需要对特权用户进行身份验证。 目前,此身份验证的密码以明文形式保存在源代码中。由于安全原因,加密密码应保存在源代码或不同的文件中。 有没有办法解决这个问题?

    const string domain = "";
    const string rooOrganizationalUnit = "";
    const string adDomain = "";
    const string adUserName = "";
    const string adPassword = "";
    private static PrincipalContext GetPrincipalContext()
    {
        PrincipalContext principalContext;

        principalContext = new PrincipalContext(ContextType.Domain, domain, rooOrganizationalUnit, ContextOptions.Negotiate, adUserName + "@" + adDomain, adPassword);

        return principalContext;
    }

(这个sn-p的代码最初取自这个site

【问题讨论】:

  • 众多选项之一是创建自定义算法来编码/解码文本,创建 DLL 并在此程序中使用编码密码调用它,因此对其他人来说并不容易除非他们获得自定义函数的源代码,否则对其进行解码。
  • 为什么不能使用集成安全性?这将消除使用或存储密码的要求。
  • 你可以把加密后的字符串放到配置文件里,有办法在编译后把字符串取出来加密,不过在配置文件里更简单。 See this blog entry 了解更多详情。
  • @Nimesh 我发现你的建议有很多问题。 customsecurity 这两个词不兼容。 Encoded 可以在几毫秒内通过基于频率的现代攻击进行暴力破解。 encodedencrypted 有很大区别。
  • @oleksii 你是对的,我同意,但我没有说这是最好的也是唯一的选择。也可以根据业务重要性选择解决方案。

标签: c# authentication encryption active-directory principalcontext


【解决方案1】:

您不想将其存储在加密或未加密的代码中。其中一种方法是将敏感数据转移到配置文件中,仅在生产环境中输入密码并在应用程序中加密该部分。

在配置文件中

<configuration>
    <appSettings>
        <add key="adPassword" value="this should be empty in source controll" />
    </appSettings>
</configuration>

在代码中

const string adPassword = ConfigurationManager.AppSettings["adPassword"];

注意事项

  • 你想加密配置文件部分,像 this 这样的东西通常可以工作
  • 如果您仍然需要提交配置文件,请使用配置文件转换,并将提交文件作为模板。密码永远不会提交给源代码管理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 2015-07-22
    • 2013-09-09
    • 2022-12-16
    • 2014-05-30
    • 2020-02-28
    • 2014-11-09
    • 2012-10-07
    相关资源
    最近更新 更多