Azure web site和web job的config文件加密方式

1.分析

由于Azure Web AppService平台的特殊性,所以在C#中原先的config加密方法DataProtectionConfigurationProviderRSAProtectedConfigurationProvider在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.测试网站是否可以正常运行。

posted @ 2018-11-03 21:27  学习虾  阅读(748)  评论(0编辑  收藏  举报