Azure web site和web job的config檔案加密方式

學習蝦發表於2018-11-03

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:LocalMachineMy

3.將解密證書匯入到windows

$mypwd = ConvertTo-SecureString -String "1234" -Force -AsPlainText

Import-PfxCertificate -FilePath ".DevConfig.pfx" -CertStoreLocation Cert:LocalMachineMy -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.測試網站是否可以正常執行。

相關文章