在web.config ASP.NET MVC中覆盖ConnectionString

时间:2013-10-08 12:53:00

标签: c# asp.net-mvc web-config connection-string

我正在尝试加密我的web.config文件的部分内容。(密码) 这适用于正常的Section-Keys,如:

<AppSettings>
    <add key="SomePassword" value="HI I AM ENCRYPTET"/>
<AppSettings>

在Global.asax中,我执行以下操作:

System.Configuration.ConfigurationManager.AppSettings["SomePassword"] = Decrypt(System.Configuration.ConfigurationManager.AppSettings["SomePassword"])

这很好用。但是,当我为Connection-String尝试此操作时,会抛出异常:“配置是只读的。”

所以,我无权写作。有人知道如何在不编辑Connection-String类的情况下解决这个问题吗?

谢谢&amp;问候

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式将秘密移出web.config文件:

<appSettings file="..\..\AppSettingsSecrets.config"> 

请参阅将密码和其他敏感数据部署到ASP.NET和Azure网站的最佳做法。 http://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure

对于Azure,我喜欢使用http://www.powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk来加密到磁盘的凭据。