微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用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初始化更改为线程安全,如azurehere所示

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.您只需要以管理员身份运行脚本并提供应用程序池名称

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐