网络场上使用 Blazor .Net 8 的防伪令牌 - 未使用机器密钥

问题描述 投票:0回答:1

我想在网络场(Windows + IIS)上部署 Blazor Web 应用程序 (.Net 8)。 如果我将应用程序部署在单台计算机上,它可以正常工作。 但是,当许多服务器使用负载平衡时,访问使用防伪功能的页面时会启动防伪错误:

Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery - An exception was thrown while deserializing the token. Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted.
 ---> System.Security.Cryptography.CryptographicException: The key {d1a4d3f6-a151-4829-8561-e7e49e3c1341} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning

我已经阅读了这篇关于 防伪造令牌和机器密钥的文章,并应用了作者的建议(这意味着在所有服务器上的 IIS 上强制使用相同的机器密钥),但不幸的是它并没有解决问题。

我错过了什么吗?

谢谢你

blazor antiforgerytoken web-farm
1个回答
0
投票

您需要将数据保护密钥环存储在某些持久存储中,例如数据库或某个地方的文件。

我写了一篇关于如何将其存储在 Azure Key Vault 中的博客文章(作为学习练习) : 将 ASP.NET Core 数据保护密钥环存储在 Azure Key Vault 中

但是,这并不是生产质量的例子。我建议您使用此处所述的更强化的存储替代方案之一:https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-7.0

下图显示了生产环境中的示例以及您是否希望跨服务实例共享 cookie。

© www.soinside.com 2019 - 2024. All rights reserved.