问题描述
我的日志中出现错误。我一整天的时间都在寻找解决方案,但找不到符合我要求的解决方案。
这是日志错误
严重性= [错误],ipaddress = xxxx, subprocess = Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery, description =反序列化令牌时抛出异常。 Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: 反伪造令牌无法解密。 ---> System.Security.Cryptography.CryptographicException:密钥 在钥匙圈中找不到{xxxxxxxxxx}。在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte [] protectedData,布尔的allowOperationsOnRevokedKeys,UnprotectStatus& 状态) Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte [] protectedData,布尔值ignoreRevocationErrors,布尔值& requireMigration,Boolean&wasRevoked) Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte [] protectedData)位于 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)在 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)在 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.GetCookietokenDoesNotthrow(HttpContext httpContext)
"Certificates": {
"StoreName": "My","StoreLocation": "LocalMachine"
"SerialNumber": "xxxxxxxxxxxx"
},private X509Certificate2 LCertificate()
{
var storeName = Configuration["Certificates:StoreName"];
var storeLocation = Configuration["Certificates:StoreLocation"];
string serialNumber = Configuration["Certificates: SerialNumber"];
using(X509Store store = new X509Store(storeName,storeLocation))
{
var certificates = store.Certificates
.Find(X509FindType.FindBySerialNumber,serialNumber,acceptValidCertOnly);
return certificates[0];
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer
.AddSigningCredential(new X509Certificate2(LCertificate()))
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel model)
{
解决方法
如果
- 您的应用托管在多台服务器上
- 尚未配置共享数据保护
- 您没有使用粘性会话
当用户向服务器A请求带有表单的页面,然后将表单提交给服务器B时,就会发生这种情况。
如果也可能在单个IIS服务器上发生
- 用户请求带有表单的页面
- 您重新启动服务器
- 用户提交表单
这样做的原因是重新启动会导致将新的密钥环加载到内存中,并且表单内的防伪密钥不再生效。
可以通过在应用程序池中选中“加载用户配置文件”来解决后一种情况。