asp.net-mvc - MVC 6 网络农场 : The antiforgery token could not be decrypted

标签 asp.net-mvc iis asp.net-core asp.net-core-mvc

我在 webfarm 场景(带有多个 AppServer 的 ARR 前端)中运行 MVC 6 (vNext)。服务器关联已关闭。
当我在应用服务器之间从一个请求跳到另一个请求时出现错误

CryptographicException: The key {3275ccad-973d-43ca-930f-fbac4d276640} was not found in the key ring.

InvalidOperationException: The antiforgery token could not be decrypted.


以前,我相信这是通过在 web.config 中设置静态 MachineKey 来处理的。
据我了解,我们现在已经转移到一个新的 DataProtection API,我尝试了以下操作,认为应用程序名称被用作某种种子:
        services.AddDataProtection();
        services.ConfigureDataProtection(configure =>
        {                
            configure.SetApplicationName("WebAppName");
        });
这不能解决问题。
知道如何在 vNext 中解决这个问题吗?

最佳答案

解释

您需要重复使用相同的 key 。

如果您在 Azure 上, key 由 %HOME%\ASP.NET\DataProtection-Keys 上的 NAS 类型存储同步。 .

对于本地运行的应用程序,它们存储在 %LOCALAPPDATA%\ASP.NET\DataProtection-Keys 中。运行应用程序或存储在注册表中(如果它在 IIS 中执行)的用户的信息。

如果以上都不匹配,则为进程的生命周期生成 key 。

解决方案

所以第一个选项不可用(仅限 Azure)。但是,您可以同步来自 %LOCALAPPDATA%\ASP.NET\DataProtection-Keys 的 key 。在运行您的应用程序的每台机器上运行您的应用程序的用户。

但更好的是,您可以将其指向这样的网络共享:

sc.ConfigureDataProtection(configure =>
{
    // persist keys to a specific directory
    configure.PersistKeysToFileSystem(new DirectoryInfo(@"Z:\temp-keys\"));
});

这将允许您在保持安全性的同时进行扩展。

重要提示:您的 key 将每 90 天到期一次。经常再生它们很重要。

你可以使用这段代码来改变它,但越短,你就越安全。

services.ConfigureDataProtection(configure =>
{
    // use 14-day lifetime instead of 90-day lifetime
    configure.SetDefaultKeyLifetime(TimeSpan.FromDays(14));
});

来源
  • Key Encryption at Rest
  • Default Settings
  • 关于asp.net-mvc - MVC 6 网络农场 : The antiforgery token could not be decrypted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319802/

    相关文章:

    c# - 当首先使用带有数据注释的 MVC 代码时,如何在显示名称中包含 html 标记?

    c# - 在重新启动 dotnet 错误之前等待文件更改

    asp.net - MVCReportViewer 帮助 - 无法将参数传递给报表 - ViewBag 问题

    iis - HTTP 错误 401.3 - 未经授权

    c# - 从 appSettings.json 获取值(value)?

    asp.net-mvc - 使用 HTML.Grid 显示子对象

    wcf - iis 8 不接受 IP 地址作为主机名

    html - 在本地 IIS 中部署 Bot 并将其包含在网页的自定义聊天中

    c# - 为什么 HttpContext.User.Identity.Name 在使用 ASP.NET Core 时对于等效于工作同步 Controller 的异步 Controller 显示为空?

    angular - Angular SPA 中的 OpenID Connect 与 Asp.Net Core 3.1 后端