首页 > 解决方案 > 网站发布到 IIS 后 XSRF 令牌过期

问题描述

我正在使用 ASP.NET 样板框架,我将下面的代码放在每个页面中。

@inject IAbpAntiForgeryManager AbpAntiForgeryManager

@{
    AbpAntiForgeryManager.SetCookie(Context);
}

我调用应用程序服务如下:

var xhr = abp.services.app.order.add(data);

        xhr.done(function (data) {
            alert(data);
        });

当我在 localhost 中运行时一切正常,即使我重建项目,XSRF 令牌也不会过期。每次点击【Publish website to IIS】,所有的ajax请求都会返回400 Bad Request,我猜是因为XSRF令牌已经过期,在浏览器中点击F5后一切恢复正常。这对用户来说太烦人了,我能做些什么来防止这个错误?还是自动刷新令牌?

谢谢!

更新

LOG 文件中的错误消息:

ERROR 2019-02-12 13:40:09,773 [71   ] .Antiforgery.Internal.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 {996d31d2-0fa3-4ffe-8e82-e155c1486d33} was not found in the key ring.

标签: asp.netasp.net-coreaspnetboilerplate

解决方案


根据错误,您的数据保护密钥在发布时已轮换。在此处查看有关数据保护的文档:https ://docs.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-2.2 。有很多选项如何配置它,在哪里存储密钥等。您需要更改它存储它们的方式。

有关配置数据保护的更多信息:https ://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2 。


推荐阅读