凭证存储最佳实践

时间:2009-02-03 19:45:21

标签: c# powershell windows-services

我正在编写Windows服务,需要进行经过身份验证的Web请求。该服务不会在用于发出请求的凭据的所有权下运行;这意味着我需要以某种方式存储请求的凭据。

这里的最佳做法是什么?凭证需要存储在App.config(或模拟)中;我宁愿没有用明文挂掉密码。由于密码经常更改,因此无法在密码中构建或以其他方式烘焙二进制文件。

同样的问题适用于Powershell。我需要进行经过身份验证的请求,但我不希望脚本以纯文本格式包含用于请求的凭据。

3 个答案:

答案 0 :(得分:6)

不能归功于答案:但这里有一篇名为“在.NET App Config中加密密码”的博客文章,其中包含完整代码。

http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx

答案 1 :(得分:2)

我总是参考Keith Brown的“Windows开发人员Windows安全指南”这本书。

完整文字在http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook.HomePage

上线

您想要的特定部分(存储机密信息)位于http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/HowToStoreSecretsOnAMachine.html

答案 2 :(得分:0)

选项#1 :使用<?php class identity { public $name; public $age; public function display() { return $this->name . 'is'. $this->age . 'years old'; } } ?> 方法加密App.config中用于Web.config文件的部分。这适用于Web和桌面应用程序以及依赖于使用加密部分复制Web.config文件的Web场。

深入介绍使用RSA提供程序的MSDN教程:

实施例

ProtectSection()

使用DPAPI提供程序的旧方法:

加密:

static public void ProtectSection()
{

    // Get the current configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);


    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");


    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);

}

要解密:

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication" -prov "RsaProtectedConfigurationProvider"

未加密的部分:

aspnet_regiis -pd "connectionStrings" -app "/SampleApplication"

加密部分:

<configuration>
  <connectionStrings>
    <add name="SampleSqlServer" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />
   </connectionStrings>
</configuration>

选项#1a 您可以将此加密移动到您的应用中,而不是使用<configuration> <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>RSA Key</KeyName> </KeyInfo> <CipherData> <CipherValue>RXO/zmmy3sR0iOJoF4ooxkFxwelVYpT0riwP2mYpR3FU+r6BPfvsqb384pohivkyNY7Dm4lPgR2bE9F7k6TblLVJFvnQu7p7d/yjnhzgHwWKMqb0M0t0Y8DOwogkDDXFxs1UxIhtknc+2a7UGtGh6Di3N572qxdfmGfQc7ZbwNE= </CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>KMNKBuV9nOid8pUvdNLY5I8R7BaEGncjkwYgshW8ClKjrXSM7zeIRmAy/cTaniu8Rfk92KVkEK83+UlQd+GQ6pycO3eM8DTM5kCyLcEiJa5XUAQv4KITBNBN6fBXsWrGuEyUDWZYm6Eijl8DqRDb11i+StkBLlHPyyhbnCAsXdz5CaqVuG0obEy2xmnGQ6G3Mzr74j4ifxnyvRq7levA2sBR4lhE5M80Cd5yKEJktcPWZYM99TmyO3KYjtmRW/Ws/XO3z9z1b1KohE5Ok/YX1YV0+Uk4/yuZo0Bjk+rErG505YMfRVtxSJ4ee418ZMfp4vOaqzKrSkHPie3zIR7SuVUeYPFZbcV65BKCUlT4EtPLgi8CHu8bMBQkdWxOnQEIBeY+TerAee/SiBCrA8M/n9bpLlRJkUb+URiGLoaj+XHym//fmCclAcveKlba6vKrcbqhEjsnY2F522yaTHcc1+wXUWqif7rSIPhc0+MT1hB1SZjd8dmPgtZUyzcL51DoChy+hZ4vLzE= </CipherValue> </CipherData> </EncryptedData> </connectionStrings> </configuration> 执行加密。它只是检测您需要保护的部分是否加密,如果没有,则加密它们。在开发过程中,您需要一个选项,以避免在生产之前运行此代码。

选项#2 加密配置文件中的字符串,并使用SecureString()临时存储解密的字符串。根据您使用support SecureString的字符串的API,这可能会或可能不会为您带来更多的攻击面里程。

相关问题