编程语言
首页 > 编程语言> > c# – asp.net核心数据保护:反序列化令牌时抛出异常

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

标签:c,caching,asp-net-core,stackexchange-redis,kestrel-http-server
来源: https://codeday.me/bug/20190705/1391605.html