保护共享主机上的连接字符串
Protecting your Connection String on Shared Hosting
我正在使用 MVC5 和 EF6 创建一个网站。我也在使用共享主机来发布这个网站。现在我遇到的问题是我的连接字符串目前以纯文本形式位于 web.config 文件中。我很难找到 "direct" 我应该如何处理这个问题的答案。
我看过很多文章,比如this one。这篇文章向我展示了如何加密 web.config 的连接部分。所以我试着按照它的例子对它在那个例子中显示的邮件部分进行了加密。在我 运行 我的代码之后,我注意到我的整个 web.config 文件都改变了。
以前是这样的:
<system.net>
<mailSettings>
<smtp from="info@Site.com">
<network
host="mail.Site.com"
port="25"
userName="info@site.com"
password="password" />
</smtp>
</mailSettings>
</system.net>
现在是这样的:
<mailSettings>
<smtp 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>odapFFPDF1Fgsk2wyvbwVC4SNISqhWc9lXiAq+I/OW3wVVqBCPowxyen9M7c9+KUBkXmGSfaUVxDMlqutChv6g6VU8h4TWG3W6Tw/istjfw/UYrRsGguPiOqdvRsl9XLBmnS37v99+VX7FEA9TKb6ufC0a3Defp2MNpGTvTIR20=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>lHPPFRJAH2hIm/Ya+ABRMP5mo5rEYwL2aBJQ/DT4Q+1OZXaftutiddxxJZ4LSgw3pzi1QJpU8eOPwFVebvqFVA4cjs27l8Iqz50E/R/tBfS7e2oqdWTRsc8IFfE/xOIieMp22BuFsYEDbgnIbLdbHJnw+92zyt2lUlzJpW9epNpnb29sVQhtNJ9cPjAaYAaU</CipherValue>
</CipherData>
</EncryptedData>
</smtp>
</mailSettings>
我现在唯一的问题是如何在不解密和保存配置文件的情况下读取代码中的这些值。我不想每次需要阅读邮件设置部分甚至连接字符串部分时都重写 webconfig 文件。
如果我有这样的方法:
public static string DecryptMailSettings()
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection section = config.GetSection("system.net/mailSettings/smtp");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
return section.???;
}
return "Nothing was read";
}
如何从上面的示例中获取假设 "host" 的值。
来自文档:
ASP.NET在处理文件时自动解密Web.config文件的内容。因此,不需要额外的步骤来解密加密的配置设置以供其他 ASP.NET 功能使用或访问代码中的值。
我建议如果 encryption/decryption 工作正常,并且 'untangling' 主机名似乎很麻烦,那么只需在 web.config 中添加一个值即可。
像这样:
<appSettings>
<add key="MAILHOST" value="mail.Site.com" />
,
然后在您的代码中阅读它。
例如:
string HostName = ConfigurationManager.AppSettings["MAILHOST"].ToString();
我正在使用 MVC5 和 EF6 创建一个网站。我也在使用共享主机来发布这个网站。现在我遇到的问题是我的连接字符串目前以纯文本形式位于 web.config 文件中。我很难找到 "direct" 我应该如何处理这个问题的答案。
我看过很多文章,比如this one。这篇文章向我展示了如何加密 web.config 的连接部分。所以我试着按照它的例子对它在那个例子中显示的邮件部分进行了加密。在我 运行 我的代码之后,我注意到我的整个 web.config 文件都改变了。
以前是这样的:
<system.net>
<mailSettings>
<smtp from="info@Site.com">
<network
host="mail.Site.com"
port="25"
userName="info@site.com"
password="password" />
</smtp>
</mailSettings>
</system.net>
现在是这样的:
<mailSettings>
<smtp 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>odapFFPDF1Fgsk2wyvbwVC4SNISqhWc9lXiAq+I/OW3wVVqBCPowxyen9M7c9+KUBkXmGSfaUVxDMlqutChv6g6VU8h4TWG3W6Tw/istjfw/UYrRsGguPiOqdvRsl9XLBmnS37v99+VX7FEA9TKb6ufC0a3Defp2MNpGTvTIR20=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>lHPPFRJAH2hIm/Ya+ABRMP5mo5rEYwL2aBJQ/DT4Q+1OZXaftutiddxxJZ4LSgw3pzi1QJpU8eOPwFVebvqFVA4cjs27l8Iqz50E/R/tBfS7e2oqdWTRsc8IFfE/xOIieMp22BuFsYEDbgnIbLdbHJnw+92zyt2lUlzJpW9epNpnb29sVQhtNJ9cPjAaYAaU</CipherValue>
</CipherData>
</EncryptedData>
</smtp>
</mailSettings>
我现在唯一的问题是如何在不解密和保存配置文件的情况下读取代码中的这些值。我不想每次需要阅读邮件设置部分甚至连接字符串部分时都重写 webconfig 文件。
如果我有这样的方法:
public static string DecryptMailSettings()
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection section = config.GetSection("system.net/mailSettings/smtp");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
return section.???;
}
return "Nothing was read";
}
如何从上面的示例中获取假设 "host" 的值。
来自文档:
ASP.NET在处理文件时自动解密Web.config文件的内容。因此,不需要额外的步骤来解密加密的配置设置以供其他 ASP.NET 功能使用或访问代码中的值。
我建议如果 encryption/decryption 工作正常,并且 'untangling' 主机名似乎很麻烦,那么只需在 web.config 中添加一个值即可。
像这样:
<appSettings>
<add key="MAILHOST" value="mail.Site.com" />
,
然后在您的代码中阅读它。
例如:
string HostName = ConfigurationManager.AppSettings["MAILHOST"].ToString();