1. 分析
由于 Azure web AppService 平台的特殊性, 所以在 C# 中原先的 config 加密方法 DataProtectionConfigurationProvider 和 RSAProtectedConfigurationProvider 在 Azure 平台上面是无法使用的, 会在发布一段时间后失效或者无法解密, 所以推荐在 Azure 上采用证书的方式加密和解密 config 配置文件 (在 Azure 门户中的应用设置中的应用设置和连接字符串是采用静态加密的, 如果只是针对 WebAPP 的话推荐采用上述方式).
2. 解决方法
1. 创建加密证书, 使用 PowerShell 工具在 Windows 机器上创建证书, 命令如下 (PowerShell 需要以管理员的方式运行)
- $cert = New-SelfSignedCertificate -Type DocumentEncryptionCert -Subject "CN=DevConfig" -KeyExportPolicy Exportable -KeySpec KeyExchange
- Export-Certificate -Cert $cert -FilePath ".\DevConfig.cer"
- $mypwd = ConvertTo-SecureString -String "1234" -Force -AsPlainText
- Export-PfxCertificate -Cert $cert -FilePath ".\DevConfig.pfx" -Password $mypwd
- $cert
使用 Export-Certificate 命令将加密证书导出为 ".cer" 文件, 使用 Export-PfxCertificate 将解密证书导出为 ".pfx" 文件, 最后的命令是用来查看证书的指纹的.
2. 将加密证书导入 Windows
Import-Certificate -Filepath ".\DevConfig.cer" -CertStoreLocation cert:\LocalMachine\My
3. 将解密证书导入到 Windows
- $mypwd = ConvertTo-SecureString -String "1234" -Force -AsPlainText
- Import-PfxCertificate -FilePath ".\DevConfig.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password $mypwd
4. 设置用于加密的 Web.config 文件, 如果是 webjob 的话需要把 App.config 改成 Web.config
在 nuget 中下载 WebConfigEncrypter 这个包, 添加到项目中, 将下面的内容添加到 webconfig 中, 将指纹的值改成之前生成的证书的指纹的值
- <configuration>
- [...]
- <configProtectedData>
- <providers>
- <add name="Pkcs12Provider" thumbprint="1234123412341234123412341234123412341234" type="WebConfigEncrypter.Pkcs12ProtectedConfigurationProvider, WebConfigEncrypter" storeLocation="LocalMachine"/>
- </providers>
- </configProtectedData>
5. 加密 Web.config 中的节点 (appsettings 和 Connection strings)
打开 visual studio 命令行代码 (在开始菜单程序中找到 vs 里面就有这个命令行快捷方式), 在里面输入下列代码
aspnet_regiis -pef "connectionStrings" "webconfig 所在的绝对路径" -prov "Pkcs12Provider"
如果执行命令出错, 运行 where aspnet_regiis 命令, 将下面的这三个 dll 放到运行命令后出来的文件夹中.
- WebConfigEncrypter.dll
- System.Configuration.ConfigurationManager.dll
- System.Security.Cryptography.xml.dll
加密后的 Web.config 如下图所示
- <connectionStrings configProtectionProvider="Pkcs12Provider">
- <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#aes192-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>rsaKey</KeyName>
- </KeyInfo>
- <CipherData>
- <CipherValue>Moy/a2XO2zvnn/HZW53DyC8aAJWo16+0KmnpC4SCSmuQZU0RT+HNFEA33pAGCzve+m6MTaRzhx6jVVRoAvpSNzfYG1bU1z7a1YnbW4OGxrmYYfdWW6cZQZ57dZnL6YSAlkJ5WlqPDGUPJa6FV/hTic3x4fJYy5vdSucmO6X3opuo1998LWNkL6fIS4WkjkG/SOFbI2Qx3HHogdN670jDHKNDON1z7bFHhLNyVj7RTO3xuQN9kF4PqbFtvwm1bYXTbZpdNxu/fcXZKONSAu8HN3QX5vTRyP/I4BG+NK7TUig3gxD4tq9GR7aSSGKJyt02PiCEO0JpyyIbHZ9xbck9kw==</CipherValue>
- </CipherData>
- </EncryptedKey>
- </KeyInfo>
- <CipherData>
- <CipherValue>TeV0yJaFlEhpyZUlQoG7M3O7sfQ7uG3ndgmhxipOrwoEsrI+Zvt1NI7arefOFWGNW4CEaoLo4mKy2Kwr4ZgK+6rAwOmx1IRyheWtF7z/8+CiGOqSRXLyGEkDQBEVOWKU0Y6TaWtPu0ZM3bp5pvKaztBnthgGnrGYmigaufu5rZW1GWPtHyL2iWdAkU9iaf+AOpA/GSvoVtZmnfJ1rwy6U8PTO0h0Ws/PdkcOKuXGkx31t/Y32ivFoy7xYPnPt/Z/aNMiHvbO7faQAwuJ/NsG9G1FFRRHCqc73TUsRdKHVuf17BEp526RG6RBZtM3F3V3o0d8/sLmyrNI9tFfksB4qcWiN4P+BRtGr0iacmBfBOvAFSozfUYxjMpx+BYPOpD1pf4fMFoKxxKeJYY31XqZoQLp75RgmWhWYm8URHq4Cjs=</CipherValue>
- </CipherData>
- </EncryptedData>
- </connectionStrings>
6. 将加密成功后的应用发布到 Azure 上, 发布之前将 Web.config 中配置加密的节点改成如下, 将 storeLocation 的值变为当前用户
- ,
- <add name="Pkcs12Provider" thumbprint="1234123412341234123412341234123412341234" type="WebConfigEncrypter.Pkcs12ProtectedConfigurationProvider, WebConfigEncrypter" storeLocation="CurrentUser"/>
7. 在 Azur 中上传刚才生成好的 pfx 证书
8. 在 Azure 的应用设置中加入 key 为 WEBSITE_LOAD_CERTIFICATES,value 为 * 的键值对, 让 webapp 可以读取应用的证书.
9. 测试网站是否可以正常运行.
来源: http://www.bubuko.com/infodetail-2833409.html