ASP.NET Core アプリケーションで特定の時点から "Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor" エラーが発生しアプリケーションが実行できない

ビロコー
質問:突然 HTTP ERROR 500 が発生するようになりました
今まで問題なく動作していたWebアプリケーションが突然、HTTP ERROR 500 になってしまいました。 イベントログを確認すると、System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation. のエラーが記録されていました。 対処方法を教えてください。

現象

ある特定の日時から、以下のエラーが発生しアプリケーションが実行できなくなります。

イベントログには以下のエラーが記録されます。

エラーメッセージ
Category: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor
EventId: 54
SpanId: 1c5aa2be7a9a1dfd
TraceId: 8aea293b090eebc79313543ac8cd11e6
ParentId: 0000000000000000
RequestId: 40000028-0003-eb00-b63f-84710c7967bb
RequestPath: /(ASP.NET Core アプリケーションのパス)

An error occurred while encrypting to Windows DPAPI.

Exception: 
System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapiCore(Byte* pbSecret, UInt32 cbSecret, Byte* pbOptionalEntropy, UInt32 cbOptionalEntropy, Boolean fLocalMachine)
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapi(ISecret secret, Boolean protectToLocalMachine)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor.Encrypt(XElement plaintextElement)
エラーメッセージ
Category: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider
EventId: 48
SpanId: 1c5aa2be7a9a1dfd
TraceId: 8aea293b090eebc79313543ac8cd11e6
ParentId: 0000000000000000
RequestId: 40000028-0003-eb00-b63f-84710c7967bb
RequestPath: /(ASP.NET Core アプリケーションのパス)

An error occurred while reading the key ring.

Exception: 
System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapiCore(Byte* pbSecret, UInt32 cbSecret, Byte* pbOptionalEntropy, UInt32 cbOptionalEntropy, Boolean fLocalMachine)
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapi(ISecret secret, Boolean protectToLocalMachine)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor.Encrypt(XElement plaintextElement)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.EncryptIfNecessary(IXmlEncryptor encryptor, XElement element)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.CreateNewKey(Guid keyId, DateTimeOffset creationDate, DateTimeOffset activationDate, DateTimeOffset expirationDate)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.CreateNewKey(DateTimeOffset activationDate, DateTimeOffset expirationDate)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now, IKey keyJustAdded)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow, Boolean forceRefresh)
エラーメッセージ
Category: Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer
EventId: 2
SpanId: 1c5aa2be7a9a1dfd
TraceId: 8aea293b090eebc79313543ac8cd11e6
ParentId: 0000000000000000
RequestId: 40000028-0003-eb00-b63f-84710c7967bb
RequestPath: /(ASP.NET Core アプリケーションのパス)

Connection ID "16933534612603273255", Request ID "40000028-0003-eb00-b63f-84710c7967bb": An unhandled exception was thrown by the application.

Exception: 
System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapiCore(Byte* pbSecret, UInt32 cbSecret, Byte* pbOptionalEntropy, UInt32 cbOptionalEntropy, Boolean fLocalMachine)
   at Microsoft.AspNetCore.DataProtection.Cng.DpapiSecretSerializerHelper.ProtectWithDpapi(ISecret secret, Boolean protectToLocalMachine)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor.Encrypt(XElement plaintextElement)
   at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.EncryptIfNecessary(IXmlEncryptor encryptor, XElement element)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.CreateNewKey(Guid keyId, DateTimeOffset creationDate, DateTimeOffset activationDate, DateTimeOffset expirationDate)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.CreateNewKey(DateTimeOffset activationDate, DateTimeOffset expirationDate)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now, IKey keyJustAdded)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow, Boolean forceRefresh)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext)
   at Microsoft.AspNetCore.Session.CookieProtection.Protect(IDataProtector protector, String data)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT'1.ProcessRequestAsync()


ASP.NET Core アプリケーションで特定の時点から

ASP.NET Core アプリケーションにアクセスすると、HTTP ERROR 500 が返ります。
ASP.NET Core アプリケーションで特定の時点から

原因

ASP.NET CoreのDataProtection機能が暗号化キーの生成に失敗していることが原因です。 暗号化キーの生成に失敗する理由として、ユーザープロファイルが読み込めない可能性が高いです。
Windowsのアップデートの更新によるセキュリティ設定の変更や、サーバーファイルの破損等でユーザープロファイルが読み込めない状況が想定されます。

対処法1: ユーザープロファイルの読み込み設定を確認し、ユーザープロファイルの読み込みを有効にする

インターネット インフォメーション サービス (IIS) マネージャー を起動します。 左側のツリービューの[アプリケーションプール]のノードをクリックして選択します。右側のエリアにアプリケーションプールの一覧が表示されます。 エラーが発生しているアプリケーションプールの項目をクリックしして選択し、右クリックします。 ポップアップメニューが表示されますので、[詳細設定]の項目をクリックします。
ASP.NET Core アプリケーションで特定の時点から

下図の[詳細設定]ダイアログが表示されます。
ASP.NET Core アプリケーションで特定の時点から

[ユーザープロファイルの読み込み]の設定を確認します。"False" に設定されています。
ユーザープロファイルの読み込みを有効にします。右側の値のドロップダウンリストボックスをクリックします。 ドロップダウンリストが表示されますので"True"の項目をクリックして選択します。

ASP.NET Core アプリケーションで特定の時点から ASP.NET Core アプリケーションで特定の時点から

[ユーザープロファイルの読み込み]の設定がTrueになりました。
ASP.NET Core アプリケーションで特定の時点から

ASP.NET Core アプリケーションのURLにアクセスしエラーが解消されたかを確認します。

対処法2: アプリケーションプールの実行ユーザーを変更する

別の対処法としてアプリケーションプールの実行ユーザーを変更する方法もあります。
先の手順と同様の手順で、エラーが発生しているアプリケーションプールの詳細設定ダイアログを表示します。
[プロセスモデル]カテゴリの[ID]の項目を変更します。現在は"ApplicationPoolIdentity"が設定されています。項目の右端の[...]ボタンをクリックします。
ASP.NET Core アプリケーションで特定の時点から ASP.NET Core アプリケーションで特定の時点から

[アプリケーション プール ID]のダイアログが表示されます。
[ビルトイン アカウント]のドロップダウンリストボックスをクリックします。ビルトインアカウントの一覧が表示されます。 一覧の"LocalSystem" または "NetworkService" のアカウントをクリックして選択します。
ASP.NET Core アプリケーションで特定の時点から ASP.NET Core アプリケーションで特定の時点から


IDの設定を変更しました。[OK]ボタンをクリックしてダイアログを閉じます。
ASP.NET Core アプリケーションで特定の時点から ASP.NET Core アプリケーションで特定の時点から

ASP.NET Core アプリケーションのURLにアクセスしエラーが解消されたかを確認します。

対処法3: アプリケーションプールの再作成、サーバーの再起動

アプリケーションプールが破損している場合も考えられます。 アプリケーションプールを削除し再度アプリケーションプールを作成しASP.NET Core アプリケーションに設定します。
また、IISの再起動やWindowsの再起動も実施します。

AuthorPortraitAlt
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
作成日: 2025-03-17
Copyright © 1995–2025 iPentec all rights reserverd.