繁体   English   中英

.Net Core 3.1 应用程序中出现“有效负载无效”错误的原因是什么?

[英]What Causes "The payload was invalid" Error in .Net Core 3.1 Application?

我们有一个 .Net Core 3.1 web 应用程序,它使用 Microsoft.AspNetCore.DataProtection 版本 3.1.0 来加密和解密数据。 由于错误“负载无效”,应用程序突然无法解密数据,如下所示:

[2021-08-18 08:12:19 ERR] [FoxCentral.Web.Api.ErrorController] Path: /api/botflows/2. Error: The payload was invalid.
Trace: at Microsoft.AspNetCore.DataProtection.Cng.CbcAuthenticatedEncryptor.DecryptImpl(Byte* pbCiphertext, UInt32 cbCiphertext, Byte* pbAdditionalAuthenticatedData, UInt32 cbAdditionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.Cng.Internal.CngAuthenticatedEncryptorBase.Decrypt(ArraySegment`1 ciphertext, ArraySegment`1 additionalAuthenticatedData)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)

我们使用 Entity Framework Core 将密钥存储在数据库中,并使用 X509 证书来保护密钥。 以下是我们如何在我们的应用程序中设置数据保护:

var protectionBuilder = services.AddDataProtection();

protectionBuilder.PersistKeysToDbContext<KeysContext>();

protectionBuilder.ProtectKeysWithCertificate(certificates.KeyProtectCertificate)
.UnprotectKeysWithAnyCertificate(certificates.KeyUnprotectCertificates.ToArray());

所有数据都在同一台服务器上加密和解密。 是什么导致解密失败? 如何恢复数据?

我发现密钥默认有 90 天的生命周期,这就是导致问题的原因。

我建议你使用IKeyManager来生成一个新的密钥,它也许可以帮助你恢复数据。

官方文档:

自动密钥环刷新

using Microsoft.AspNetCore.DataProtection;


services.AddDataProtection()
            .SetApplicationName("ProjectName")
            .AddKeyManagementOptions(options =>
            {
                options.NewKeyLifetime = new TimeSpan(180, 0, 0, 0);
                options.AutoGenerateKeys = true;
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM