我正在尝试发布到ftp服务器。我正在使用以下代码加密我的Web.config文件:
var exeConfigName = @"D:\Repo\mypp\WebApplication1\Web.config";
Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigName);
var section = config.GetSection("appSettings") as AppSettingsSection;
ConnectionStringsSection section2 = config.GetSection("connectionStrings") as ConnectionStringsSection;;
var section3 = config.GetSection("system.net/mailSettings/smtp") as ConfigurationSection;
if (section != null && section.SectionInformation.IsProtected) {
// Remove encryption.
section.SectionInformation.UnprotectSection();
} else {
// Encrypt the section.
if (section != null) section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
if (section2 != null && section2.SectionInformation.IsProtected) {
// Remove encryption.
section2.SectionInformation.UnprotectSection();
} else {
// Encrypt the section.
if (section2 != null) section2.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
if (section3 != null && section3.SectionInformation.IsProtected) {
// Remove encryption.
section3.SectionInformation.UnprotectSection();
} else {
//Encrypt the section
if (section3 != null) section3.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
// Save the current configuration.
config.Save();
我得到两个webconfigs,即Web.config(正常)和Web.config.cong(加密)。当我发布时,只发布了Web.config。所以我手动将加密部分添加到服务器上的Web.config中。但我得到以下错误:
使用提供程序'DataProtectionConfigurationProvider'无法解密。来自提供程序的错误消息:密钥无法在指定状态下使用。
</runtime>
<appSettings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>//error highlighted in red
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAKvzpAGMd9EWXylxQjNCcdAQAAAACAAAAAAADZgAAwAAAABAAAABPSw54k9XlD+Mv/LWO9npqAAAAAASAAACgAAAAEAAAALpLRoKXjtupmwjCJCBfEtwgAAAAO3J95kdVtev2Cvc1Chtkajg8ZNamBU7zV9EXNy4VQgoUAAAAVNrkN3oVQVy12XSbxdZVVilA/r0=</CipherValue>
我该如何解决这个问题。此外,我的bin文件夹中没有release文件夹(很奇怪)。这是我第一次发布和加密。感谢