我有一个使用mvc实体框架创建的.net Web应用程序。我使用开发人员命令提示符对aspnet_regiis.exe中的connectionStrings部分进行了加密。以下命令用于加密:
aspnet_regiis.exe -pef "connectionStrings" "C:\..webconfig path.."
上面的代码对web.config中的连接字符串部分进行了加密,但是它引发了如下异常:
ConfigurationErrorsException:无法打开RSA密钥容器。 该错误将引发以下代码:
public DataContext()
: base("LEAContext")
{
Database.SetInitializer(new Seeder());
Configuration.ProxyCreationEnabled = false;
}
加密前的连接字符串:
<connectionStrings>
<add name="LEAContext" connectionString="Data Source=.\SQLExpress;Database=LEADbServer;User ID=sa;Password=pwd@123" providerName="System.Data.SqlClient" providerName="System.Data.SqlClient" />
</connectionStrings>
加密后的连接字符串:
<connectionStrings 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>HRY3gmulN8MhNrCehjWyoBH600bGtLm0km1TvV7fCGPZjQHnST6mJ6YU4UWUr300Y9BPfhh/bwyMR2sa5zNnN4sT/XiUOCKJ6a3YvKg3LBEA/DmTWiFow5F9JllyP3OO1271l9uEuEuYcVdovqf7D3s4DWupGGi6MxFjl9tQe89HjKcvgiLYTQQp0jmFDdjcBWaXuk7ad5FENsgpTdAdUQk3zlneiq1X6NzqoA9S5v87qKIqUTwcJ6TfWvowcGqkQAh13IqtegbDwci22eoYvT/kVmsOYW38/IrHn1eaw4ZqlfH5hXJMJda9AWNyqvoYd2JILMZzZ/lHGCClfUAMlg==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>abpT46oFCWxOsJ5UTkg9WAZCrYoVlQ5fRn6RqtDgQ1udN+wmWbECnBMMxftR1Zthn2NJ2Nq546Q+7z9pyaQ9bE8oczbdOYY8g7FqWvkQbPr0f4vmsXCZGgJwa0TaCT8k82CAuRTh7oo9QZOnGPoEdV8KaB1hXwDWLQkx8y5F59FdQemwZButRd/bOXg9M3yG7qF/u4i3bH5iATYVNBzSZXn7saR1ltTooQNolIxe5RD4fR3XnpYauJfu40ag97UHeQcle+I39hKp0OS/PStBctJAg0bUa1C+</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>