加密web.config后出现错误

4
我加密了web.config中的AppSettings部分,在本地测试通过,但上传到线上后出现错误:

配置错误描述:在处理所需的配置文件时发生错误。请查看以下特定错误详细信息,并相应地修改您的配置文件。

解析器错误消息:使用提供程序“DataProtectionConfigurationProvider”解密失败。提供程序返回的错误消息:密钥不适用于指定状态。(来自 HRESULT: 0x8009000B 的异常)


Line 24: <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
Line 25:  <EncryptedData>

我使用以下子程序进行加密:
Private Sub ProtectSection(ByVal sectionName As String, ByVal provider As String)
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)

        Dim section As ConfigurationSection = config.GetSection(sectionName)

        If section IsNot Nothing AndAlso Not section.SectionInformation.IsProtected Then
            section.SectionInformation.ProtectSection(provider)
            config.Save()
        End If
    End Sub
2个回答

3

您需要使用已解密的部分进行发布。用于加密/解密的密钥是机器特定的。

要在线加密配置部分,请在global.asax的Application_Start()方法中调用ProtectSection()方法。


1
你的意思是我需要在线加密它吗? - Maen
1
是的,你正在使用的ProtectSection()方法会在应用程序首次被访问时执行此操作。 - Jeremy

1

您需要设置 MachineKey

.NET 加密使用 MachineKey 作为加密/解密种子

http://msdn.microsoft.com/en-us/library/w8h3skw9.aspx

你需要生成一个密钥并在两台机器上使用它。你不能只是自动生成它。

如果可以的话,最好直接上传未加密的文件,并在服务器上手动加密。否则,你需要完全相同的MachineKey。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接