2

当加密的连接字符串保存在外部配置文件中并由 asp.net 应用程序的 web.config 的 connectionStrings 部分中的 configSource 属性指定时,如何在 C# 代码中读取加密的连接字符串?

外部配置文件应该只有 connectionStrings 节点,但是当它被加密时,configDataProvider 节点也应该存在于同一个文件中。因此它不能在 configSource 属性中使用。

我已经加密了连接字符串,并希望将它放在外部配置文件中。如何处理?

感谢您的任何指示。

4

1 回答 1

4

在您的 app.config 中:

<configuration>
    <connectionStrings configSource="foo.config" />
    ...
</configuration>

在你的foo.config

<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
        <CipherData>
            <CipherValue>
                AQAAANCMnd8BFdE....
            </CipherValue>
        </CipherData>
    </EncryptedData>
</connectionStrings>

并在您的代码中:

ConfigurationManager.ConnectionStrings["someKey"]
于 2012-06-15T17:33:56.187 回答