加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – asp.net核心数据保护:反序列化令牌时抛出异常

发布时间:2020-12-15 22:44:06 所属栏目:百科 来源:网络整理
导读:我使用redis作为数据保护存储在webfarm上运行asp.net核心. 我的应用程序负载很重,所以我不断从redis得到以下错误: Timeout performing EVAL,inst: 9,queue: 1448,qu: 0,qs: 1448,qc: 0, wr: 0,wq: 0,in: 65536,ar: 0,clientName: xxxx 使用microsoft recomm
我使用redis作为数据保护存储在webfarm上运行asp.net核心.

我的应用程序负载很重,所以我不断从redis得到以下错误:

Timeout performing EVAL,inst: 9,queue: 1448,qu: 0,qs: 1448,qc: 0,
wr: 0,wq: 0,in: 65536,ar: 0,clientName: xxxx

使用microsoft recommendation在startup.cs初始化redis的方式是

var redis = ConnectionMultiplexer.Connect("<URI>");
services.AddDataProtection()
    .PersistKeysToRedis(redis,"DataProtection-Keys");

所以在阅读一些关于不是redis线程安全的文章时我将redis初始化更改为线程安全,如azure和here所示

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => {
    return ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});

public static ConnectionMultiplexer Connection {
    get {
        return lazyConnection.Value;
    }
}

但现在问题是每隔一秒左右我就会收到以下错误:

The antiforgery token could not be decrypted. —>
System.Security.Cryptography.CryptographicException: The key {xxxxxxx}
was not found in the key ring.

要么

An exception was thrown while deserializing the token.

要么

Error unprotecting the session cookie.

要么

System.Security.Cryptography.CryptographicException: The key {xxxxx}
was not found in the key ring.

什么问题,我该怎么办?

我应该再次更改初始化吗?但是应用程序将在重负载下停止.

更新

更多信息:

我不小心为redis使用了错误的连接字符串,并再次将其更改为正确的连接字符串.

当然可能有其他错误导致此错误.

我现在如何处理和缓解此错误?

错误很明显,可能无法在另一个开发环境中重现它.

但应该有办法处理这种错误.

解决方法

为避免数据保护错误,您可以运行PowerShell脚本. https://raw.githubusercontent.com/aspnet/DataProtection/dev/Provision-AutoGenKeys.ps1.您只需要以管理员身份运行脚本并提供应用程序池名称

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读