Azure Web Appsでマシン キーに基づく暗号化が利用できない


解説

Shops App Serviceが再起動した後で、以下のメッセージがログに出力される可能性があります。

System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.UnprotectWithDpapiCore(Byte* pbProtectedData, UInt32 cbProtectedData, Byte* pbOptionalEntropy, UInt32 cbOptionalEntropy)
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.UnprotectWithDpapi(Byte[] protectedSecret)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor.Decrypt(XElement encryptedElement)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngGcmAuthenticatedEncryptorFactory.CreateEncryptorInstance(IKey key)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.CreateEncryptor()
   at Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver.CanCreateAuthenticatedEncryptor(IKey key)

この事象は、あるマシン キーを使用して暗号化されたキーを、他のマシン キーを使って復号化した場合に発生します。

解決策

CryptographicExceptionの再発を防ぐには、キー暗号化設定をMachineからDPAPI-CERTに切り替えます。

  1. 信頼された証明書を、同じ暗号化キーを共有する必要があるAzure Web Appに配置します。
  2. Azure Web Appが証明書を読み込むことを確認します。
  3. 各Commerce Engineロールのconfig.jsonファイルで、EncryptionProtectionTypeアプリケーション設定の値を、DPAPI-CERTに設定します。
  4. 各Commerce Engineロールのconfig.jsonファイルで、EncryptionCertificateHashアプリケーション設定の値を、貴社の証明書のサムプリントに設定します。
  5. 各Commerce Engineロールのconfig.jsonファイルで、EncryptionKeyStorageLocationアプリケーション設定の値を、証明書がインストールされている全てのAzure Web Appにアクセス可能なロケーションに設定します。
  6. Web Appで、Commerce EngineのApp Servicesを再起動します。